Angular 角度路由器可选参数销毁/初始化组件
我正在使用@angular/router 我的路线:Angular 角度路由器可选参数销毁/初始化组件,angular,angular2-routing,Angular,Angular2 Routing,我正在使用@angular/router 我的路线: { path: '', component: MyComponent, }, { path: ':id', component: MyComponent } } 是否有任何选项可以在两个状态(“”,:id')之间导航而不使用destory/initMyComponent 例如: url:空, 调用ngOnInit() url:/some\u id, 调
{
path: '',
component: MyComponent,
},
{
path: ':id',
component: MyComponent
}
}
是否有任何选项可以在两个状态(“”,:id')之间导航而不使用destory/initMyComponent
例如:
url:空,
调用ngOnInit()
url:/some\u id
,
调用
ngOnDestory()
,ngOnInit()
(销毁/构建MyComponent)您可以使用ActivatedRoute
注入器进行此操作,如下所示
export default class MyComponent {
constructor(
private route: ActivatedRoute,
private router: Router) {}
ngOnInit() {
this.sub = this.route
.params
.subscribe(params => {
if(params['id']){
/// perform some opeartion with id
} else{
// perform some other opeartion
}
});
}
对您想要做的是
无参数
和id=1
它应该如何工作。我的建议是使用两个不同的组件。我需要两个选项使用相同的组件…为什么?有什么正当理由吗?在我的应用程序上,打电话给ngOnInit很贵。无论如何,如果不是ngOnInit()
您将在哪里处理路由器更改??