I have created the following route guard:
// #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
}
I use this as follows:
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);
I have both imported into my one module:
@NgModule({
imports: [
BrowserModule,
RouterModule,
HttpClientModule,
MatSnackBarModule,
ApplicationRoutes
],
declarations: [
ApplicationComponent,
SignInComponent
],
providers: [
{ provide: AuthenticationGuard }
],
bootstrap: [ApplicationComponent]
})
export class ApplicationModule {}
However, when I run my app, Angular says:
Error: Uncaught (in promise): Error: Invalid CanActivate guard
Error: Invalid CanActivate guard
In what fashion is my guard invalid?
You need to pass the providers
array like below
@NgModule({
imports: [
BrowserModule,
RouterModule,
HttpClientModule,
MatSnackBarModule,
ApplicationRoutes
],
declarations: [
ApplicationComponent,
SignInComponent
],
providers: [AuthenticationGuard],
bootstrap: [ApplicationComponent]
})
export class ApplicationModule {}