【发布时间】:2018-07-31 22:57:14
【问题描述】:
我正在尝试使用 Angular 路由器,但在空路径上遇到了问题。这是我的路线:
const routes: Routes = [
{ path: 'feed', loadChildren: './feed/feed.module#FeedModule', canLoad: [AuthGuardService] },
{ path: 'login', component: LoginPage },
{ path: 'register', component: RegisterPage },
{ path: '', redirectTo: '/feed', pathMatch: 'full' },
{ path: '**', redirectTo: '/' }
];
我的 AuthGuardService 有一个方法 canLoad,它总是返回 false 并重定向到 '/login' 路径:
...
@Injectable()
export class AuthGuardService implements CanLoad {
constructor(private router: Router) {
}
canLoad(route: Route): boolean {
this.router.navigate([ '/login' ]);
return false;
}
}
当我转到“localhost:4200/feed”时,我被重定向到“/login”。
但如果我转到 'localhost:4200/',则忽略身份验证保护并显示我的提要模块的组件。
你知道为什么吗?
谢谢!
【问题讨论】:
-
把
canLoad改成canActivate能用吗?
标签: angular angular-routing auth-guard