Angular2 CustomReuseStrategy在使用参数

时间:2017-01-17 18:50:06

标签: angular angular2-routing

我使用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.navigate还是router.navigateByUrl,行为都是一样的。

1 个答案:

答案 0 :(得分:0)

基于此处的源代码:

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

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