我将 Angular-14 代码升级到 Angular-15。然后我有这个代码:
授权卫士:
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { ToastrService } from 'ngx-toastr';
import { AuthService } from 'src/app/features/auth/services/auth.service';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private toastr: ToastrService, private router: Router) { }
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
):
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree>
| boolean
| UrlTree {
if (!this.authService.isLoggedIn()) {
this.toastr.info('Please Log In!');
this.router.navigate(['/auth']);
return false;
}
// logged in, so return true
this.authService.isLoggedIn();
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我得到这个错误:
CanActivate is deprecated
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
谢谢。
| 归档时间: |
|
| 查看次数: |
58882 次 |
| 最近记录: |