我是 angular 7 的新手,现在正在尝试实现 CanActive,但我收到错误:
谁能指导我克服这个问题。我的代码示例如下:
auth.guard.ts :
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth-service.service';
import {Router} from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private auth: AuthService,
private myRoute: Router){
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.auth.isLoggednIn()){
return true;
} else {
this.myRoute.navigate(["login"]);
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud) angular7 ×1