我尝试在 Angular 中编写 AuthGuard,但出现以下错误:
类型 'typeof AuthServiceService' 不可分配给类型 '(request?: HttpRequest) => string | 承诺'。类型 'typeof AuthServiceService' 提供不匹配的签名 '(request?: HttpRequest): string
这是我的 auth-service.service.ts:
@Injectable({
providedIn: 'root'
})
export class AuthServiceService {
constructor(
private http:HttpClient,
public jwtHelper: JwtHelperService) { }
login(data):Observable<any>{
return this.http.post(baseUrl + 'ApplicationUser/login' ,data)
}
handleError(error: HttpErrorResponse) {
return throwError(error);
}
public isAuthenticated(): boolean {
const token = localStorage.getItem('token');
return !this.jwtHelper.isTokenExpired(token);
}
}
这是我的 AuthGuard 服务:
import { AuthServiceService } from 'src/services/auth/auth/auth-service.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(public auth: AuthServiceService, public router: Router) { }
canActivate(): boolean {
if (!this.auth.isAuthenticated()) {
this.router.navigate(['']);
return false;
}
return true;
}
}
我在 tokenGetter 上的这一行收到此错误:
const JWT_Module_Options: JwtModuleOptions = {
config: {
tokenGetter: AuthServiceService
}
};
有人可以帮我 AuthService 的响应应该是什么样子吗?