1

大家好 ! 这就是我正在做的事情:

https://github.com/jonny720/do-here-client/blob/master/do-here/app/%40shared/services/aute.interceptor.ts

我在使用 HTTP_INTERCEPTOR 时遇到问题,我想构建拦截器,该拦截器将为我的应用程序中的每个 http 请求添加标头。

但是当我在 app.module.ts 添加这一行时,应用程序不会启动。

提供者:[{提供:HTTP_INTERCEPTORS,useClass:AuthInterceptor,multi:true}]

你认为可能是什么问题?谢谢你 :)

auth.interceptor.ts :

import { Injectable } from "@angular/core";
import {HttpRequest,HttpHandler,HttpEvent,HttpInterceptor} from '@angular/common/http';
import { AuthGuard } from './auth-guard.service';
import { Observable } from "../../../node_modules/rxjs";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  // constructor(public auth: AuthGuard){}
    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      // const authToken = appSettings.getString('token');
      // console.log("token is  "+authToken);

      //   request = request.clone({
      //       setHeaders: {
      //         Authorization: `Bearer ${appSettings.getString('token')}`
      //       }
      //     });
      console.log("intercepted", request);
          return next.handle(request);
        }
}

app.module.ts :

import { NgModule, NO_ERRORS_SCHEMA, Injectable, ModuleWithProviders } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { AppComponent } from "./app.component";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";
import { SharedModule } from "~/@shared/shared.module";
import { PagesModule } from "~/pages/pages.module";
import { APP_ROUTES } from "~/app.routes";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { HTTP_INTERCEPTORS } from "../node_modules/@angular/common/http";
import { AuthInterceptor} from "~/@shared/services/aute.interceptor";

@NgModule({
    bootstrap: [AppComponent],
    imports: [
        NativeScriptHttpClientModule,
        NativeScriptRouterModule,
        NativeScriptRouterModule.forRoot(APP_ROUTES),
        NativeScriptModule,
        NativeScriptFormsModule,
        SharedModule.forRoot(),
        PagesModule
    ],
    declarations: [AppComponent,],
    providers: [ {provide: HTTP_INTERCEPTORS, useClass:AuthInterceptor, multi:true}],
    schemas: [NO_ERRORS_SCHEMA]
})
export class AppModule { }
4

1 回答 1

1

尝试放入共享模块

shared.module.ts:

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

在 app.module.ts 中:

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


imports: [

        SharedModule
   ]
于 2018-08-28T06:52:22.000 回答