基本上我目前有以下Angular模块:
landing
admin
core
shared
我想要的是在AuthenticationGuard
模块中注册一个名为shared
的新警卫,并在不同的模块中提供它。
目前它只能起作用,如果我在landing-module
(这是我的引导程序)中注册守卫,如果我在admin.module
或shared.module
中注册它。
如果我这样做,则会收到错误消息,说明以下内容:
guard
的注册是通过相应模块的providers
数组完成的。
我的目标是能够在所有模块中使用它。
从core
模块中的admin
注入服务没有问题 - 所以我认为guards
和services
之间必须存在差异?
目前一些相关文件看起来像那样(为简洁起见缩短了):
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HomeComponent } from './home/home.component';
import { LandingRoutingModule } from './landing.routing.module';
import { SharedModule } from '../shared/shared.module';
import { CoreModule } from '../core/core.module';
import { SecurityModule } from '../security/security.module';
import { AdminModule } from '../admin/admin.module';
@NgModule({
declarations: [
HomeComponent
],
imports: [
SharedModule.forRoot(),
CoreModule.forRoot(),
SecurityModule,
LandingRoutingModule
],
providers: [],
bootstrap: [HomeComponent]
})
export class AppModule { }
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { HomeComponent } from './home/home.component'
import { NotFoundComponent } from './../shared/components/not-found/not-found.component';
const appRoutes: Routes = [
{
path : '',
redirectTo : '/login',
pathMatch: 'full'
},
{
path : 'admin',
loadChildren: 'app/modules/admin/admin.module#AdminModule'
},
{
path: '**',
component: NotFoundComponent
}
];
@NgModule({
declarations: [],
imports: [
HttpClientModule,
RouterModule.forRoot(
appRoutes,
{ enableTracing: true }
)
],
exports: [
RouterModule
],
})
export class LandingRoutingModule { }
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
import { AdminLandingComponent } from './admin-landing/admin-
landing.component'
import { AdminChildComponent } from './admin-child/admin-child.component'
import { AdminRoutingModule } from './admin.routing.module';
@NgModule({
declarations: [
AdminLandingComponent,
AdminChildComponent
],
imports: [
CommonModule,
AdminRoutingModule
],
})
export class AdminModule { }
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AdminLandingComponent } from './admin-landing/admin-
landing.component';
import { AdminChildComponent } from './admin-child/admin-child.component';
import { AuthenticationGuard } from '../shared/guards/auth-guard.service'
const adminRoutes: Routes = [
{
path: '',
component: AdminLandingComponent,
canActivate: [AuthenticationGuard],
children: [
{
path: '',
children: [
{ path: 'child', component: AdminChildComponent }
]
}
]
}
];
@NgModule({
declarations: [],
imports: [
RouterModule.forChild(adminRoutes)
],
exports: [
RouterModule
],
})
export class AdminRoutingModule { }
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NotFoundComponent } from './components/not-found/not-
found.component'
import { AuthenticationGuard } from './guards/auth-guard.service';
@NgModule({
declarations: [
NotFoundComponent,
],
imports: [
CommonModule
],
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [AuthenticationGuard]
};
}
}
import { Injectable } from '@angular/core';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanActivateChild,
NavigationExtras,
CanLoad,
Route
} from '@angular/router';
import { AuthenticationService } from '../../core/services/authentication-service/authentication.service';
@Injectable()
export class AuthenticationGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(private authService: AuthenticationService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true;
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
canLoad(route: Route): boolean {
return true;
}
}
答案 0 :(得分:2)
您需要在AuthenticationService
数组中的模块装饰器中导入providers
。这就是错误所表明的内容!
根据您的具体情况,我不会在任何模块中添加AuthenticationService
作为提供者。除非它在app.modules.ts
中,否则不会显示在此处。