我创建了以下路由守卫:
// #region Imports
/* Angular */
import {Injectable} from '@angular/core';
import {CanActivate} from '@angular/router';
// #endregion Imports
@Injectable()
export class AuthenticationGuard implements CanActivate {
// #region Operations
public canActivate() : boolean {
return true;
}
// #endregion Operations
}
我按如下方式使用它:
const routes : Routes = [
{
path: '',
redirectTo: 'admin',
pathMatch: 'full'
},
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthenticationGuard]
},
{
path: 'signin',
component: SignInComponent
}
];
export const ApplicationRoutes : ModuleWithProviders = RouterModule.forRoot(routes);
我都已经导入了我的一个模块:
@NgModule({
imports: [
BrowserModule,
RouterModule,
HttpClientModule,
MatSnackBarModule,
ApplicationRoutes
],
declarations: [
ApplicationComponent,
SignInComponent
],
providers: [
{ provide: AuthenticationGuard }
],
bootstrap: [ApplicationComponent]
})
export class ApplicationModule {}
但是,当我运行我的应用程序时,Angular说:
Error: Uncaught (in promise): Error: Invalid CanActivate guard
Error: Invalid CanActivate guard
我的监护人以哪种方式无效?
答案 0 :(得分:1)
您需要像下面一样传递providers
数组
@NgModule({
imports: [
BrowserModule,
RouterModule,
HttpClientModule,
MatSnackBarModule,
ApplicationRoutes
],
declarations: [
ApplicationComponent,
SignInComponent
],
providers: [AuthenticationGuard],
bootstrap: [ApplicationComponent]
})
export class ApplicationModule {}