2017-01-05 96 views
7

當我回到相同的組件時,我開始瞭解粘滯路由以重新連接早期的組件數據。我是通過在這裏查看這個https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx博客來實現演示https://plnkr.co/edit/KVlRi9PtPeOpvn8bECBi?p=preview ......是嗎?可能有適用條件,以便routerreusestrategy僅適用於少數組件?有條件地爲angular2路由應用路由器重用策略

+0

可能的重複[如何實現RouteReuseStrategy應該針對Angular 2中的特定路由進行分配](http://stackoverflow.com/questions/41280471/how-to-implement-routereusestrategy-shoulddetach-for-specific-routes-in-angular ) –

回答

11

您需要的原液只有輕微的修改:https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx

添加shouldDetach標誌路線:

const appRoutes: Routes = [ 
    { path: 'crisis-center', component: CrisisListComponent, data: { shouldDetach: true } }, 
    ... 
]; 

並修改shouldDetach方法CustomReuseStrategy

public shouldDetach(route: ActivatedRouteSnapshot): boolean { 
    return route.data && (route.data as any).shouldDetach; 
} 

這裏是你的plunker更新:https://plnkr.co/edit/otbZBuRmGYQXeY6b4Sfp?p=preview

+1

感謝您的提示,這有助於我有條件的路線重用工作! –

+0

我很高興它爲你工作,保持編碼;) –