我有一个router.navigate无法正常工作的组件(解锁)。我不知道为什么路由器不重定向页面。它仅在此组件中发生。有任何想法吗 ?
submit() {
if (!this.unlockForm.valid) { return; }
this.isLoading = true;
this.userDataService.unlockCpo(this.model).subscribe(data => {
this.oauthService.refreshToken().then(done => {
this.translateService.get('UnlockAccount.UnlockSuccess').subscribe((res: string) => {
this.toastr.success(res);
this.isLoading = false;
this.router.navigate(['/dashboard']); // there is not working but debuger goes on the line and no errors thrown..
});
}
还有我的app.routing.ts
{
path: 'unlock',
component: UnlockComponent
},
{
path: 'logout',
component: LogoutComponent
},
{
path: 'dashboard',
canActivate: [CloudConnectivityGuard, AccessGuard],
component: DashboardComponent
},
这是canActive方法
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) {
// const hasIdToken = this.oauthService.hasValidIdToken();
const hasIdToken = true;
const hasAccessToken = this.oauthService.hasValidAccessToken();
if (!hasIdToken || !hasAccessToken) {
this.router.navigate(['/login']);
return false;
}
if (!this.userService.isCpo()) {
this.router.navigate(['/unlock']);
}
return true;
}
}
答案 0 :(得分:1)
您还必须在防护中创建路由器的对象。
此外,如果您返回 true ,它将导航到该页面。
constructor(protected router: Router) { }
canActivate() {
const hasIdToken = true;
const hasAccessToken = this.oauthService.hasValidAccessToken();
if (!hasIdToken || !hasAccessToken) {
this.router.navigate(['/login']);
return false;
}
if (!this.userService.isCpo()) {
this.router.navigate(['/unlock']);
return false;
}
return true;
}