Angular-仅在延迟加载的模块上添加身份验证拦截器

时间:2018-07-19 16:30:42

标签: angular

我有3个模块。应用,报告和连接。

要访问报告模块,您需要拥有一个jwt,以便我有一个auth拦截器来处理此问题。目前,拦截器是在我的 app.module.ts 中定义的,但我只希望在我的延迟加载的 reports.module.ts

中使用auth拦截器

当前设置。

  

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { SharedModule } from './shared/shared.module';
import { CoreModule } from './core/core.module';

import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AuthInterceptor } from './core/interceptors/auth.interceptor';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        HttpClientModule,
        SharedModule,
        CoreModule.forRoot(),
    ],
     providers: [{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

新设置

  

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { SharedModule } from './shared/shared.module';
import { CoreModule } from './core/core.module';

import { HttpClientModule } from '@angular/common/http';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        HttpClientModule,
        SharedModule,
        CoreModule.forRoot(),
    ],

    bootstrap: [AppComponent]
})
export class AppModule { }
  

reports.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UpperCasePipe } from '@angular/common';
import { DatePipe } from '@angular/common';
import { TitleCasePipe } from '@angular/common';

import { ReportsRoutingModule } from './reports-routing.module';

import { ReportsComponent } from './reports.component';

import { SharedModule } from './../shared/shared.module';
...

import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AuthInterceptor } from './../core/interceptors/auth.interceptor';


@NgModule({
    imports: [
        CommonModule,
        SharedModule,
        ReportsRoutingModule,
        HttpClientModule,
    ],
    declarations: [
       ...
    ],
    entryComponents: [
      ...,

    ],
    providers: [DatePipe, UpperCasePipe, TitleCasePipe, { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }],

})
export class ReportsModule { }

但是,新设置不起作用,并且永远不会触发auth拦截器。 (我什至尝试注释掉报告中的HTTPClientModule导入。)

0 个答案:

没有答案