所以我试图为我的 web 应用程序实现 Route Guards,但遇到了很多错误。我做错了什么?
我的 webapp 由多个路由组成,其中一个是仪表板,需要通过登录组件进行保护。这正是我尝试实施但失败的方法。我的登录组件从后端生成一个 JWT 令牌并将其添加到 LocalStorage(以对象“令牌”的格式:“”)。
我的dashboard.component.ts:
import { Component, OnInit } from '@angular/core';
import { User } from '../models/user.model';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
user: User;
constructor() { }
ngOnInit() {
}
canDeactivate(): Promise<any> | boolean {
if (localStorage.getItem('currentUserToken') === 'token') {
return (true);
} else {
return (false);
}
}
}
我的 auth.guard.ts:
import {Injectable} from '@angular/core';
import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot} from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate, CanLoad {
constructor(private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.checkToken();
}
canLoad(route: Route): boolean {
return this.checkToken();
}
checkToken() {
if (localStorage.getItem('currentUserToken')) {
return true;
} else {
this.router.navigateByUrl('/avior/login');
return false;
}
}
}
我的 can-exit.guard.ts:
import {Injectable} from '@angular/core';
import {CanDeactivate} from '@angular/router';
import {CanExit} from './canExit';
@Injectable()
export class CanExitGuard implements CanDeactivate<CanExit> {
canDeactivate(component: CanExit) {
if (component.canDeactivate) {
return component.canDeactivate();
}
return true;
}
}
我的 canExit.ts:
import {Observable} from 'rxjs';
export interface CanExit {
canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
浏览器中的错误是:
DashboardComponent.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'id' of undefined
DashboardComponent.ngfactory.js? [sm]:1 ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 1, nodeDef: {…}, elDef: {…}, elView: {…}}
ERROR TypeError: Cannot read property 'id' of undefined
ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 1, nodeDef: {…}, elDef: {…}, elView: {…}}
更新:StackBlitz: https ://stackblitz.com/edit/angular-v9u7nw?file=src%2Fapp%2Fapp.routing.ts