2016-03-28 13 views
1

Angular2'nin Yönlendiricisi'nin OnReuse ve CanReuse bölümlerini anlamaya çalışıyorum ve duvara çarpıyorum. Kodumu, here belgelerinden sonra modelledim, ancak bir nedenden dolayı, rota değiştiğinde arama yöntemlerini alamıyorum. Yanlış yaptığımdan emin değilim.yönlendiriciCanReuse ve yönlendirici Rota değiştiğindeOnReuse çağrılmadı

import {Component, NgZone} from 'angular2/core'; 
import {CanReuse, OnReuse, ComponentInstruction} from 'angular2/router'; 
import {NgClass} from 'angular2/common'; 

@Component({ 
    selector: 'product-table', 
    templateUrl: __resourcePath + '/html/product-list.html', 
    directives: [NgClass] 
}) 
export class ProductTable implements CanReuse, OnReuse { 

    public storeProducts: Store_Product__c[] = []; 
    public selectedStore: string; 
    public selectedCategory: string; 
    public errors: { [id: string]: string } = {}; 


    constructor(private zone: NgZone) { 

    } 

    routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) { 
     console.log('routerCanReuse fired'); 
     return true; 
    } 

    routerOnReuse(next: ComponentInstruction, prev: ComponentInstruction) { 
     console.log('Reusing!'); 
     console.log(next); 
     this.selectedStore = next.params['selectedStore']; 
     this.selectedCategory = next.params['selectedCategory']; 
     this.storeProducts = next.params['storeProducts']; 
    } 
} 
+0

'NgClass' Direktiflere eklenmeye gerek yoktur. "NgClass", varsayılan olarak [COMMON_DIRECTIVES] (https://angular.io/docs/ts/latest/api/common/COMMON_DIRECTIVES-let.html) –

+0

'un bir parçası olarak kullanılabilir. tarayıcı konsolunda herhangi bir hata var mı? –

+0

@ GünterZöchbauer hiçbir hata ne olursa olsun – watzon

cevap

1

dokümanlar bu yeterince açık değil düşünüyorum, yoksa sadece bir yanlış aranır

app.component.ts

import {Component, OnInit, NgZone, View} from 'angular2/core'; 
import {Location, RouteConfig, RouterLink, ROUTER_DIRECTIVES} from 'angular2/router'; 
import {ProductTable} from './product-table.component'; 
import {AddProduct} from './add-product.component'; 

@Component({ 
    selector: 'app' 
}) 
@RouteConfig([ 
    { path: '/', name: 'ProductTable', component: ProductTable, useAsDefault: true }, 
    { path: '/add-product', name: 'AddProduct', component: AddProduct } 
]) 
@View({ 
    templateUrl: __resourcePath + '/html/app.html', 
    directives: [ROUTER_DIRECTIVES, RouterLink] 
}) 
export class AppComponent { 

    public __resourcePath = __resourcePath; 

    constructor(public location: Location) { 

    } 

} 

ürüne table.component.ts: İşte benim kod doc.

Köşeli, yönlendirici farklı türde bir bileşene giderse bileşenin yeniden kullanılabilir olduğunu düşünmez. Aşağıdaki açıklama açısal source

elde edilir, bir navigasyon tanınması aşamasında {@link Yönlendirici} tarafından çağrılır [yönlendirici-prizinin yeniden yöntemi olup]. Yeni alt bileşen, var olan alt bileşenine göre farklı Tür içeriyorsa, bu false olarak çözülür. Yeni bileşen farklı bir Tür olduğunda eski bir bileşenini yeniden kullanamazsınız. Aksi takdirde, bu yöntem varsa alt bileşen için routerCanReuse kancasına temsil eder veya kanca mevcut değilse true değerine döner.

ProductTable ile ProductTable arasında gezinme olasılığınız yoktur. Yani CanReuse kanca asla denir. Ancak, stratejiyi, bir öğenin ayrıntılarından bir sonraki öğenin ayrıntılarına geçebileceğiniz ProductDetail gibi bileşenlerde yeniden kullanmayı deneyebilirsiniz.

+0

Ayrıca bakınız [yönlendirici-çıkış belgesi] (https://angular.io/docs/ts/latest/api/router /RouterOutlet-directive.html), –

+0

aynı şekilde kafa karıştırıcı gibi görünüyor Bu yüzden, şu anda iki farklı bileşenle bunu yapmak mümkün değil mi? – watzon

+0

Belki bu DynamicComponentLoader tarafından yapılabilir. Ama hiçbir fikrim yok. –

İlgili konular