0

我创建了以下路由守卫:

// #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

我的警卫以什么方式无效?

4

1 回答 1

2

您需要providers像下面这样传递数组

@NgModule({
 imports:      [
  BrowserModule,
  RouterModule,
  HttpClientModule,
  MatSnackBarModule,
  ApplicationRoutes
],
declarations: [
  ApplicationComponent,
  SignInComponent
],
providers: [AuthenticationGuard],
bootstrap: [ApplicationComponent]
})
export class ApplicationModule {}
于 2019-04-26T19:33:34.163 回答