这是守门员:
// my.guard.ts
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
Injectable()
export class MyGuard implements CanActivate {
canActivate(next: ActivatedRouteSnapshot, prev: RouterStateSnapshot) {
console.log('GUARD:', next, prev);
return true;
}
}
这是路由器:
import { RouterConfig } from '@angular/router';
import { MyGuard } from './guards/my.guard';
export const AppRoutes : RouterConfig = [
{
path: 'home',
component: HomeComponent,
canActivate: [MyGuard]
}
];
当页面加载时,我得到:
EXCEPTION: Error: Uncaught (in promise): No provider for MyGuard!
我见过的任何教程都没有提到如何将 MyGuard 作为提供者注入。我该怎么做呢?