यह उत्तर कोडवायरियर द्वारा official documentation से जुड़ा हुआ है। , आप के साथ अनुरोध क्लोन कर सकते हैं
import {NgModule} from '@angular/core';
import {HTTP_INTERCEPTORS} from '@angular/common/http';
@NgModule({
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: NoopInterceptor,
multi: true,
}],
})
export class AppModule {}
एक प्राधिकरण हैडर जोड़ने के लिए:
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
@Injectable()
export class NoopInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req);
}
}
जो आप तो इतनी तरह से अपने ऐप्लिकेशन में एकीकृत कर सकते हैं:
कोणीय आप एक HttpInterceptor बनाने की अनुमति देता बदले गए हेडर:
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Get the auth header from the service.
const authHeader = this.auth.getAuthorizationHeader();
// Clone the request to add the new header.
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
}
}
ध्यान दें कि इंटरसेप्टर चाई की तरह कार्य करते हैं n, ताकि आप विभिन्न कार्यों को करने के लिए एकाधिक इंटरसेप्टर सेट कर सकें। इंटरसेप्टर के निर्माता को
http://blog.ninja-squad.com/2017/07/17/http-client-module/ इस उपयोगी प्रतीत होता है – john
आप देख सकते हैं https://github.com/auth0/angular2-jwt उदाहरण के रूप में /tree/v1.0 1.x शाखा। – estus
आधिकारिक कोणीय दस्तावेज बहुत अच्छा है: https://angular.io/guide/http#setting-new-headers – CodeWarrior