Angular2 CustomReuseStrategy 在使用参数布线时创建组件的新实例



我正在使用Angular2 CustomReuseStrategy类来保存组件实例。当我第一次路由到组件时,会创建一个新实例。这按预期工作。但是,当我使用新参数路由到同一组件时,将创建该组件的新实例,并且 onInit 函数再次执行。我希望组件使用相同的实例,但更新 URL 参数。有什么想法吗?我被困在这一点上。

这是我的路线。

{ path: 'tiles', component: DisplayTileData },
{ path: 'tiles/:master/:filters', canActivate: [AuthGuard], component: DisplayTileData } 

以下是导航到这些路由的方法。

addNewMasterPath(newMasterVariable) {
    this.master = this.master + '-' + newMasterVariable;
    var newMap = this.variable.map(items => { return items}).join('-');
    this.router.navigate(['tiles', this.master, newMap]);
}
onSelectAddNewParameter(newParameter) {
    this.variable.push(newParameter);
    var newMap = this.variable.map(items => { return items}).join('-');
    this.router.navigateByUrl('/tiles/this.master/newMap');
}

需要明确的是,无论我使用router.naving还是router.navigateByUrl,行为都是相同的。

基于此处的源代码:

https://github.com/angular/angular/blob/2.4.1/modules/%40angular/router/src/router.ts#L293-L798

我认为没有一种方法可以导航不重新加载组件。 是否可以没有这些参数的更具体的路由,并通过@Input将查询信息传递给组件?

最新更新