【发布时间】:2017-07-28 02:39:55
【问题描述】:
您好,我有一个奇怪的问题,不知道如何解决。所以我有服务可以检查你在访问网络应用程序中的某个 url 时是否登录。如果不是,它会重定向到主页。
我的服务`
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable()
export class AuthService implements CanActivate {
private userLogedin: string = localStorage.getItem('AUTHENTICATION');
constructor(private router: Router) {}
canActivate() {
if(this.userLogedin === 'true') {
return true;
}
this.router.navigateByUrl('/');
return false;
}
}
我的模块路由
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
import { LandingPageComponent } from 'app/components/LandingPageComponent/LandingPageComponent';
const landingPageRoutes: Routes = [
{path: '', component: LandingPageComponent, pathMatch: 'full'}
];
export const landingPageRouting: ModuleWithProviders = RouterModule.forChild(landingPageRoutes);
Router canactivate module.routing
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
import { ProfilePageComponent } from 'app/components/ProfilePageComponent/ProfilePageComponent';
import { AuthService } from 'app/shared/AuthService';
const profilePageRoutes: Routes = [
{path: 'profile', component: ProfilePageComponent, canActivate: [AuthService] }
];
export const profilePageRouting: ModuleWithProviders = RouterModule.forChild(profilePageRoutes);
但是当你重定向到主页时,它给了我一个空白页。
【问题讨论】:
-
canActivate 守卫在哪里应用?
-
我更新问题
标签: angular typescript routing canactivate