首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么在canActivate()之前调用ngOnInit()?

为什么在canActivate()之前调用ngOnInit()?
EN

Stack Overflow用户
提问于 2018-06-08 02:22:23
回答 1查看 6K关注 0票数 4

我使用了路由保护,特别是canActivate()方法,但是Angular在调用canActivate之前调用了我的根AppComponentngOnInit()

AppComponent可以在模板中呈现一些数据之前,我必须等待canActivate中的一些数据。

我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2020-04-11 03:46:25

对于我来说,我在我的应用程序组件中监听了ROUTE_NAVIGATED事件,如下所示

我使用ngrx/router-store来监听这些路由器操作。

代码语言:javascript
复制
  // 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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50747839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档