gpt4 book ai didi

angular - 拦截器没有拦截,尽管只有一个 HttpClientModule

转载 作者:行者123 更新时间:2023-12-04 10:31:26 25 4
gpt4 key购买 nike

我在 Angular 中有应用程序,我只有一个 HttpClientModule,但是当我在构造函数中提供 HttpClient 时,如下所示:

export class UserService {

constructor(private http: HttpClient /** <---- this one is ok, and requests are intercepted */) {
const i='i';
}

但在另一个模块中我也有类似的构造函数
export class TableComponent implements OnInit {

...
...
constructor(private http: HttpClient /** <---- this one is not ok, and requests are not intercepted */) {

}

我的 app.module
@NgModule({
declarations: [AppComponent],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptorRefresh,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
AppRoutingModule,
MainAppModule,
TableModule,

表格模块内容
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {TableHeaderComponent} from './table-header/table-header.component';
import {TableComponent} from './table/table.component';
import {TablePaginationComponent} from './table-pagination/table-pagination.component';
import {TableFilterComponent} from './table-filter/table-filter.component';

@NgModule({
declarations: [TableHeaderComponent, TableComponent, TablePaginationComponent, TableFilterComponent],
imports: [
CommonModule,
],
exports: [TableHeaderComponent, TableComponent, TablePaginationComponent, TableFilterComponent],
})
export class TableModule {
}

主应用模块
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {UsersComponent} from './users/users.component';
import {MainAppRoutingModule} from './main-app-routing.module';
import {LayoutComponent} from './layout/layout.component';
import {ThemeModule} from '../../@theme/theme.module';
import {NbCardModule} from '@nebular/theme';
import {Ng2SmartTableModule} from 'ng2-smart-table';
import {TableModule} from '../table/table.module';

@NgModule({
declarations: [UsersComponent, LayoutComponent],
imports: [
CommonModule, MainAppRoutingModule, ThemeModule, NbCardModule, Ng2SmartTableModule, TableModule,
],
})
export class MainAppModule {
}

我究竟做错了什么?

最佳答案

只进口HttpClientModule一旦在您的应用程序中并在同一个地方提供拦截器。

根据docs :

Because interceptors are (optional) dependencies of the HttpClient service, you must provide them in the same injector (or a parent of the injector) that provides HttpClient. Interceptors provided after DI creates the HttpClient are ignored.

关于angular - 拦截器没有拦截,尽管只有一个 HttpClientModule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60409884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com