Angular 变量值在拦截方法-HTTP拦截器中不可见
我试图使用HTTP拦截器在HTTP请求的头中传递变量值。但这并没有发生 我已尝试将变量从AppComponent传递到服务。我可以看到变量值,但在Intercept方法下的同一服务中,我无法看到Angular 变量值在拦截方法-HTTP拦截器中不可见,angular,angular2-services,angular4-httpclient,Angular,Angular2 Services,Angular4 Httpclient,我试图使用HTTP拦截器在HTTP请求的头中传递变量值。但这并没有发生 我已尝试将变量从AppComponent传递到服务。我可以看到变量值,但在Intercept方法下的同一服务中,我无法看到 import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; import { Injectable } from
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import * as $ from 'jquery';
@Injectable({
providedIn:'root'
})
export class AppInterceptorService implements HttpInterceptor{
etag : string
headers : HttpHeaders
constructor() {}
getEtag(etag : string) {
if(etag) {
this.etag = etag;
console.log("Etag from Interceptor :"+ this.etag)
}
else {
this.etag = '*'
}
}
handleError(error : HttpErrorResponse) {
console.log("Error Occured")
return throwError(error)
}
intercept(req: HttpRequest<any>, next: HttpHandler,): Observable<HttpEvent<any>> {
console.log("interceptetag : " + this.etag)
if(req.method === "GET"){
this.headers = new HttpHeaders ({
'Content-Type' : 'application/json;odata=verbose',
'Accept' : 'application/json;odata=verbose',
'X-RequestDigest' : $("#__REQUESTDIGEST").val(),
'X-HTTP-Method': 'MERGE',
'IF-MATCH': "40",
})
}
if(req.method === "POST"){
console.log("Etag form POST :"+this.etag)
this.headers = new HttpHeaders ({
'Content-Type' : 'application/json;odata=verbose',
'Accept' : 'application/json;odata=verbose',
'X-RequestDigest' : $("#__REQUESTDIGEST").val(),
'X-HTTP-Method': 'MERGE',
'IF-MATCH': this.etag,
})
}
const clone = req.clone({'headers' : this.headers})
return next.handle(clone)
.pipe(
retry (1),
catchError(this.handleError)
)
}
}
应用模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule,FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';
import { SharePointService } from './services/sharepointservice.service';
import { AppInterceptorService } from './services/app-interceptor.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ReactiveFormsModule,
FormsModule,
HttpClientModule
],
providers: [SharePointService,
{'provide' : HTTP_INTERCEPTORS,
'useClass' : AppInterceptorService,
'multi' : true}],
bootstrap: [AppComponent]
})
export class AppModule { }
根据,intercept有一个返回类型
Observable,根据,intercept有一个返回类型Observable我忘了在帖子中提到这一点。我使用了返回类型。这是否解决了您的问题?如果没有,你还面临什么问题?查看上面正在工作的Stackblitz,看看你是否遗漏了什么。谢谢Nash!我看到了你的代码&它与我在应用程序中使用的代码相同。我不明白为什么这不起作用!它在截取方法中显示为未定义。我忘了在帖子中提到这一点。我使用了返回类型。这是否解决了您的问题?如果没有,你还面临什么问题?查看上面正在工作的Stackblitz,看看你是否遗漏了什么。谢谢Nash!我看到了你的代码&它与我在应用程序中使用的代码相同。我不明白为什么这不起作用!它在intercept方法中显示为未定义
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule,FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';
import { SharePointService } from './services/sharepointservice.service';
import { AppInterceptorService } from './services/app-interceptor.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ReactiveFormsModule,
FormsModule,
HttpClientModule
],
providers: [SharePointService,
{'provide' : HTTP_INTERCEPTORS,
'useClass' : AppInterceptorService,
'multi' : true}],
bootstrap: [AppComponent]
})
export class AppModule { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log("interceptetag : " + this.etag)
return new Observable<any>();
}