Angular2 CustomReuseStrategy在使用参数进行路由时创建组件的新实例

前端之家收集整理的这篇文章主要介绍了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.navigate还是router.navigateByUrl,行为都是一样的.

解决方法

根据这里的源代码

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

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

猜你在找的Angularjs相关文章