首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 4 CanActivateChild不工作

Angular 4 CanActivateChild不工作
EN

Stack Overflow用户
提问于 2017-06-08 06:17:00
回答 0查看 1.1K关注 0票数 0

我试图使用CanActivateChild限制对我的路由系统的访问,但是当我试图阻止用户导航到一组子状态时,RouteGuard不起作用,只能在CanActivate上起作用。这是我的路由模块:

代码语言:javascript
复制
export const routes = [
    { path: "", redirectTo: "/signin", pathMatch: "full" },
    { path: "signin", component: SigninComponent },
    {
        path: "user",
        component: UserComponente,
        canActivate: [AuthGuard],
        canActivateChild: [AuthGuard],
        children: [
            { path: "profile", component: ProfileComponent, },
            { path: "address", component: AddressComponent, },
        ]
    },
    { path: "test", component: TestComponent, canActivate: [AuthGuard] },
];

在本例中,如果我尝试访问路由test,我可以看到AuthGuard正在执行,函数canActivate被调用。

但是,当我尝试导航到任何子路由(配置文件或地址)时,什么也没有发生。canActivatecanActivateChild都不会在AuthGuard上被调用。

这是我的防护文件:

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild } from '@angular/router';
import { getString } from 'application-settings';
import { AppConfig } from './../config';

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
    constructor() { }

    canActivate() {
        console.log('canActivate executing.');

        if (!getString(AppConfig.authToken)) {
            alert('You need to be logged in.');
            return false;
        }
        return true;
    }

    canActivateChild() {
        console.log('canActivateChild executing.');
        return this.canActivate();
    }
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44423662

复制
相关文章

相似问题

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