如何在Angular 2中链接CanActivate

时间:2016-12-20 21:13:27

标签: angular rxjs angular2-template

我有以下路线定义......

const COMMON_ROUTES = [
    {
        path: "login",
        component: LoginComponent,
        pathMatch: "prefix",
        canActivate: [ CanActivateIfAnonymous ],
        children: [
            {
                path: "",
                component: PasswordLoginComponent,
                canActivate: [CanActivateIfPassword],
            },
            {
                path: "pki",
                component: PkiLoginComponent,
                canActivate: [CanActivateIfPki],
            },
        ],
    },
];

然后我有了CanActivates ......

export class CanActivateIfPassword implements CanActivate {
    constructor(private loginSettingsService: LoginSettingService,
                private router: Router) { }
    canActivate(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot,
    ) {
        console.log("Password Check");
        return this.loginSettingsService.loginSettings.map(settings =>{
            console.log("Password Check Finished");
            return true;
        });
    }
}
export class CanActivateIfPki implements CanActivate {
    constructor(private loginSettingsService: LoginSettingService) { }
    canActivate(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot,
    ) {
        console.log("PKI Check");
        return this.loginSettingsService.loginSettings.map((settings) => {
            console.log("PKI Check Finished");
            return settings["pki"];
        });
    }
}

但是当我跑步时,我会期待......

Password Check
Password Check Finished
PKI Check
PKI Check Finished

但我得到......

Password Check
PKI Check
PKI Check Finished

我注意到loginSettings是一个值而不是一个可观察的,所以其他的observable是第一个。我会尽快发布一个弹药。

Plunker

这个打印出像......

Password Check
PKI Check
PKI Check Finished
Password Check Finished

这也不是我想要的。所以我会查看遗失的陈述,但我的问题仍然适用。但它实际上是有道理的,因为在我的代码中PKI正在重定向。

0 个答案:

没有答案