Angular-拦截器未在延迟加载的模块中加载

Kay*_*Kay 6 angular angular-httpclient

我创建了一个拦截器,该拦截器将令牌附加到仅在功能延迟加载的模块中进行api调用所需的授权标头中。

但是,我不认为拦截器被称为是因为在reports模块中没有显示console.logs。

我读过其他问题,认为这可能与HTTPClientModule有关。此HttpClientModule仅在我的主app.module中进行过一次初始化。

我如何使拦截器仅适用于延迟加载的功能模块。

验证拦截器

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/do';

import { AuthService } from './../services/auth/auth.service';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(private auth: AuthService) {
        console.log('start interceptor');
     }

    intercept(req: HttpRequest<any>, next: HttpHandler) {

        console.log('intercepting');

        const authToken = this.auth.getProfileToken();

        console.log(authToken);

        const authReq = req.clone({
            headers: req.headers.set('Authorization', authToken)
        });

        return next.handle(authReq);

    }

}
Run Code Online (Sandbox Code Playgroud)

report.module.ts

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

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

...

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

..

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

@NgModule({
    imports: [
        CommonModule,
        SharedModule,
        ReportsRoutingModule,
    ],
    declarations: [
        ...
    ],
    entryComponents: [
        ...
    ],
    providers: [DatePipe, UpperCasePipe,
        { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
    ]
})
export class ReportsModule { }
Run Code Online (Sandbox Code Playgroud)

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(),
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from '../app/core/guard/auth/auth.guard';

const routes: Routes = [
    {
        path: 'reports',
        loadChildren: './reports/reports.module#ReportsModule',
        canActivate: [
            AuthGuard
        ]
    }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule],
    providers: [AuthGuard]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

小智 14

HTTP_INTERCEPTORS当延迟加载的模块导入另一个HttpClientModule自行导入的模块时,提供者令牌将被重置。

因此HttpClientModule可以包含在您的应用程序模块中,并且只需要一次。

https://angular.io/guide/http#setup-installing-the-module

  • 对于大多数用例,HttpClientModule 应该并且应该仅包含在应用程序模块中。 (4认同)
  • 这是如何回答这个问题的?OP询问如何使延迟加载功能模块注册的http拦截器工作。 (2认同)

小智 6

每次在延迟加载模块中加载模块时,HttpClient都会将服务的新实例注入到尚未配置为使用AppModule. 因此,在模块级别而不是应用程序级别添加拦截器。