【发布时间】:2019-10-06 06:39:40
【问题描述】:
From the Angular documentation on canActivate,如果canActivate 函数最终返回true,您似乎只能使用canActivate 守卫来允许继续执行路由。
有没有办法说,“只有在 canActivate 类的计算结果为 false 时才继续这条路线”?
例如,不允许登录的用户访问登录页面,我尝试了这个但它不起作用:
export const routes: Route[] = [
{ path: 'log-in', component: LoginComponent, canActivate: [ !UserLoggedInGuard ] },
我在控制台中收到此错误:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError[false]:
StaticInjectorError[false]:
NullInjectorError: No provider for false!
Error: StaticInjectorError[false]:
StaticInjectorError[false]:
NullInjectorError: No provider for false!
【问题讨论】:
-
你不能否定一个类型,那是完全错误的
-
用这个登录页面的特殊情况。我做了一个完全独立的守卫,比如
AuthRedirectGuard,它检查用户是否登录。如果为真,则重定向到登录页面,否则继续登录。
标签: angular typescript angular-routing canactivate angular-guards