未触发对 ActivatedRoute.params 的订阅
Subscription to ActivatedRoute.params isnt triggered
问题是当我在两个相似的 URL 之间导航时我的组件没有重绘。例如,当用户查看 http://localhost:8088/#/app/route/study/6/detail
然后导航到 http://localhost:8088/#/app/route/study/7/detail
时,StudyComponent
会正确重新加载。但是,DetailComponent
未重新加载。我觉得是因为detail的params
没变,所以Angular觉得不重装就可以了。但在我的例子中,细节 取决于父参数 。
我该如何解决这个问题?
我的路线:
const studyRoutes: Routes = [
{
path: '', component: RouteComponent,
children: [
{path: 'study/:id', component: StudyComponent,
children: [
{path: 'detail', component: DetailComponent },
{path: 'parts', component: PartsComponent }
]
}
]
}
];
我所有的组件都是这样初始化的:
ngOnInit() {
this.loader = this.route.params.subscribe(params => this.onLoad());
}
ngOnDestroy() {
this.loader.unsubscribe();
}
当我在 不同研究的细节之间导航时 onLoad
从未在 DetailComponent
中被调用。
要在 DetailComponent
级别响应对 :id
参数的更改,您需要进行以下更改:
// detail.component.ts
ngOnInit() {
this.loader = this.route.parent.params.subscribe(params => this.onLoad());
}
这是因为您的路由 中的路径 detail
没有 定义 :id
参数,它的父级定义了。因此,更改会反映在路由器状态树中的那个级别。
问题是当我在两个相似的 URL 之间导航时我的组件没有重绘。例如,当用户查看 http://localhost:8088/#/app/route/study/6/detail
然后导航到 http://localhost:8088/#/app/route/study/7/detail
时,StudyComponent
会正确重新加载。但是,DetailComponent
未重新加载。我觉得是因为detail的params
没变,所以Angular觉得不重装就可以了。但在我的例子中,细节 取决于父参数 。
我该如何解决这个问题?
我的路线:
const studyRoutes: Routes = [
{
path: '', component: RouteComponent,
children: [
{path: 'study/:id', component: StudyComponent,
children: [
{path: 'detail', component: DetailComponent },
{path: 'parts', component: PartsComponent }
]
}
]
}
];
我所有的组件都是这样初始化的:
ngOnInit() {
this.loader = this.route.params.subscribe(params => this.onLoad());
}
ngOnDestroy() {
this.loader.unsubscribe();
}
当我在 不同研究的细节之间导航时 onLoad
从未在 DetailComponent
中被调用。
要在 DetailComponent
级别响应对 :id
参数的更改,您需要进行以下更改:
// detail.component.ts
ngOnInit() {
this.loader = this.route.parent.params.subscribe(params => this.onLoad());
}
这是因为您的路由 中的路径 detail
没有 定义 :id
参数,它的父级定义了。因此,更改会反映在路由器状态树中的那个级别。