【问题标题】:Angular - Provide guards across different modulesAngular - 跨不同模块提供防护
【发布时间】:2018-03-30 15:02:35
【问题描述】:

目前我基本上有以下 Angular 模块:

  1. landing
  2. admin
  3. core
  4. shared

我想要的是在 shared 模块中注册一个名为 AuthenticationGuard 的新守卫,并在不同的模块中提供它。

目前它只有在我在landing-module(这是我引导的那个)中注册警卫时才有效,如果我在admin.moduleshared.module中注册它也无效。

如果我这样做,我会收到一条错误消息,说明如下:

guard 的注册是通过相应模块的providers 数组完成的。

我的目标是能够在所有模块中使用它。

admin 模块中的core 注入服务没有问题 - 所以我认为guardsservices 之间肯定存在差异?

目前一些相关文件看起来像这样(为简洁起见而缩短):

landing.module.ts

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 { }

landing.routing.module

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 { }

admin.module

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 { }

admin.routing.module

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 { }

shared.module

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]
    };
  }
}

auth-guard.service

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;
    }
}

【问题讨论】:

  • 那么,共享模块中的守卫在哪里以及如何提供?发布它的定义。
  • AuthenticationService 怎么样,您是否将其添加到提供程序数组中?在哪个模块中?
  • 很好的输入 - 我已将其添加到主题中。我添加了guard shared 模块的实现。
  • @HansMusterWhatElse 正如 Fateh 所指出的:您没有在任何地方提供 AuthenticationService。

标签: angular angular-router angular-module angular-router-guards


【解决方案1】:

您需要在providers 数组中的模块装饰器中导入AuthenticationService。这就是那个错误所表明的!

根据您在此处的情况,我没有看到在您的任何模块中添加了 AuthenticationService 作为提供程序。除非它在您的app.modules.ts 中,否则不会在此处显示。

【讨论】:

  • 出于某种原因,我认为错误提到了AuthenticationGuard - 我会看看那个!
猜你喜欢
  • 2018-03-27
  • 2017-04-15
  • 2021-05-26
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多