加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – canActivate取决于父路线解析

发布时间:2020-12-17 07:26:02 所属栏目:安全 来源:网络整理
导读:我有这样的路线设置: export const APP_ROUTES: Routes = [ { path: '',resolve: { user: CurrentUserResolver },children: [ { path: ':sectionKey',canActivate: [SectionAccessGuard],children: [ { path: 'dashboard',component: DashboardComponent }
我有这样的路线设置:
export const APP_ROUTES: Routes = [
    {
        path: '',resolve: {
            user: CurrentUserResolver
        },children: [
            {
                path: ':sectionKey',canActivate: [SectionAccessGuard],children: [
                    {
                        path: 'dashboard',component: DashboardComponent
                    }
                ]
            }
        ]
    }
]

父路由将通过HTTP调用检索用户,并且我希望我的子路由:sectionKey仅在当前用户有权访问时才被激活.问题是,在快照完全填充之前,我的canActivate可以调用SectionAccessGuard:

@Injectable()
export default class SectionAccessGuard implements CanActivate {
    canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot) {
        console.log(route.data);
        console.log(route);
        return true;
    }
}

在控制台中,第一个条目什么都没有;但是,第二个条目最终将填充用户字段.

我知道canActivate方法可以返回一个Observable< boolean>同样,但我没有看到任何关于如何等待,直到解决完成的钩子.

我在这里想念的是什么?我觉得这应该是非常直接的.我目前正在使用angular v4.0.0-rc.2

我发布此消息已经过去了大约8个月,所以我会更新我们最终的结果:

通过解析在路由器中存储数据开始变得非常尴尬取决于组件的位置(this.route.parent.parent.parent.data.user),所以我们不再使用解析,而是使用NgRx来存储一种信息.

然后我们的警卫负责触发数据的获取,并等待它完成.像这样的东西:

@Injectable()
export class AuthenticationGuard implements CanActivate {
    constructor(private router: Router,private store: Store<ApplicationState>) { }

    canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot) {
        this.store.dispatch(new AuthenticateAction());

        return this.store
            .select('auth')
            .filter((s) => !!s && !s.authenticating)
            .map((s) => s.authenticated)
            .take(1);
    }
}

在州内有几个标志(经过身份验证和验证),告诉警卫我们在这个过程中的位置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读