我使用了路由保护,特别是canActivate()
方法,但是Angular在调用canActivate
之前调用了我的根AppComponent
的ngOnInit()
。
在AppComponent
可以在模板中呈现一些数据之前,我必须等待canActivate
中的一些数据。
我该怎么做呢?
发布于 2020-04-11 03:46:25
对于我来说,我在我的应用程序组件中监听了ROUTE_NAVIGATED事件,如下所示
我使用ngrx/router-store来监听这些路由器操作。
// app.component.ts
public ngOnInit(): void {
// grab the action stream
this.actions$.pipe(
// Only pay attention to completed router
ofType(ROUTER_NAVIGATED),
// Now I can guarantee that my resolve has completed, as the router has finsihed
// Switch
switchMap(() => {
// Now switch to get the stuff I was waiting for
return this.someService.getStuff();
})
// Need to subscribe to actions as we are in the component, not in an effect
// I suppose we should unsubscribe, but the app component will never destroy as far as I am aware so will always be listening
).subscribe();
https://stackoverflow.com/questions/50747839
复制相似问题