Angular5 HttpInterceptor depending on the result of an Observable

前端 未结 2 1799
终归单人心
终归单人心 2021-01-02 18:09

I am trying to implement using Angular5 an HttpInterceptor to inject an Authorization header in all HTTP requests.

I rely on a third party library (ADAL

2条回答
  •  既然无缘
    2021-01-02 18:50

    possible issue is here

     constructor(private auth: AuthService) { }
    

    the provider is not available inside the interceptor

    solution

    use Injector to inject your service

    // 1. import the injector
    import { Injectable, Injector } from '@angular/core';
    // 2. create a constant of your service
    private auth: AuthService;
    // 3. add injector into contsructor; remove authService from constructor
    constructor(private injector: Injector) { }
    // 4. now create instance of your service inside intercept()
    intercept() {
    const auth = this.injector.get(AuthService);
    // 5. now call method on this auth object
    

提交回复
热议问题