我有以下带有参数 T1 的测试路线:
{
path: 'Test/:T1',
component: TestComponent
},
当我从“Test/1”路由到“Test/2”时,我的 TestComponent
没有重新初始化。这是 Angular 路由器的问题吗?
我正在使用 "@angular/router": "3.0.0-beta.1"
最佳答案
这是目前唯一支持的行为。有计划使其可配置 https://github.com/angular/angular/issues/9811
您可以订阅参数更改并在那里进行初始化
export class MyComponent {
constructor(private route : ActivatedRoute,
private r : Router) {}
reloadWithNewId(id:number) {
this.r.navigateByUrl('my/' + id + '/view');
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.paramsChanged(params['id']);
});
}
paramsChanged(id) {
console.log(id);
// do stuff with id
}
}
关于Angular2 Component 不会为参数化路由重新初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38847642/