angular - CanActivate 问题 TS2720
问题描述
我有一个关于 CanActivate 的问题
import { AuthService } from './auth.service';
import { CanActivate } from '@angular/router/src/utils/preactivation';
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router)
{
}
canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot )
: Observable<boolean> | Promise<boolean> |boolean {
if (this.authService.isAuth) {
return true;
}else{
this.router.navigate(['/auth']);
}
}
}
它告诉我这个:
src/app/services/auth-guard.service.ts(10,14) 中的错误:错误 TS2720:“AuthGuard”类错误地实现了“CanActivate”类。您的意思是扩展“可以激活”并将其成员作为子类继承吗?
“AuthGuard”类型缺少“CanActivate”类型的以下属性:路径、路由
但我不明白为什么?
非常感谢大家!
解决方案
您CanActivate
从错误的位置导入了界面。它应该是
import { CanActivate } from '@angular/router';
return false
在else
声明中也是一个好主意。
推荐阅读
- python - 如何在替换字符串的同时保留原始字符串的部分内容?
- html - 响应式导航栏不会崩溃
- node.js - 如何将 TypeORM 与 Angular Universal 一起使用?
- java - 在 GridBagLayout 中正确定位 JButton | 爪哇 8
- c++ - 如何导入 dll 并导出数组?
- javascript - Discord.JS 嵌入问题
- linux - 如果二进制文件不在 $PATH 中,我的 bash 如何找到二进制文件?
- python - 如何在 Keras 中实现 CNN2D+ LSTM 模型进行图像分类?
- nestjs - 如何使用 Nestjs 验证枚举值数组
- c# - Swagger 令牌错误 (Unsupported_Grant_Type)