【发布时间】:2016-11-02 13:05:59
【问题描述】:
我在某些路由中使用了登录保护(例如“/profile”)。看起来像这样:
canActivate() {
if (this.user.islogin) return true;
this.router.navigate(['']);
}
如果用户未登录,它会将用户重定向到主页并尝试访问某些路由。它工作正常。 用户可以从任何页面注销。我希望我的应用程序具有下一个行为: 如果用户在“/profile”页面并单击“注销”,则应通过 canActivate() 方法将他重定向到主页。但是在这种情况下不会调用此方法。我应该怎么做才能运行 canActivate()(重新加载页面,手动调用)? 我的注销方法:
logout() {
localStorage.removeItem('auth_token');
this.islogin = false;
}
我尝试在注销方法中添加this.router.navigate([this.router.url]);,但结果是一样的。
只有当我重新加载页面时,angular2 才会重定向我。
什么是正确的方法?
【问题讨论】:
标签: angular