在使用canActivate过程中,如果它没有正确的重定向到目标网站,可能有以下几个原因:
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [ { path: 'route', canActivate: [AuthGuard], component: MyComponent } ];
import { Injectable } from '@angular/core'; import { CanActivate, Router } from '@angular/router'; import { AuthService } from './auth.service';
@Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate {
constructor( private authService: AuthService, private router: Router ) { }
canActivate(): boolean { if (!this.authService.isAuthenticated()) { this.router.navigate(['/login']); return false; } return true; }
}
以上是常见的问题和解决方法。如果还没有解决问题,可以参考官方文档进行排除。