我正在使用角度 CanActivate Authguard 接口来保护我的组件。
@Injectable()
export class AuthGuard implements CanActivate{
constructor(private router: Router, private authService: AuthenticationService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
this.authService.isLoggedIn.take(1).map((isLoggedIn : boolean) => {
if(!isLoggedIn){
this.router.navigate(['/login']);
return false;
}
return true;
})
this.router.navigate(['/login']);
return false;
}
}
我像这样将它添加到我的路由器配置中。
const appRoutes: Routes = [
{path : '',redirectTo : 'login',pathMatch : 'full'},
{ path: 'home', component: HomeComponent,canActivate : [AuthGuard] }
]
我还将它添加到提供者数组中。
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [AuthGuard,
ExpenseService,SellDetailService,AuthenticationService],
styleUrls: ['./app.component.css']
})
但是当我运行应用程序时,它会出现以下错误
StaticInjectorError(AppModule)[AuthGuard]:
StaticInjectorError(Platform: core)[AuthGuard]: NullInjectorError: No provider for AuthGuard!
我想我正确地实施了它,但它不起作用。我做错了什么???