首页 > 解决方案 > 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”类型的以下属性:路径、路由

但我不明白为什么?

非常感谢大家!

标签: angular

解决方案


CanActivate从错误的位置导入了界面。它应该是

import { CanActivate } from '@angular/router';

return falseelse声明中也是一个好主意。


推荐阅读