Angular - Set headers for every request

前端 未结 19 1599
[愿得一人]
[愿得一人] 2020-11-22 07:43

I need to set some Authorization headers after the user has logged in, for every subsequent request.


To set headers for a particular request,



        
19条回答
  •  囚心锁ツ
    2020-11-22 08:32

    How about Keeping a Separate Service like follows

                import {Injectable} from '@angular/core';
                import {Headers, Http, RequestOptions} from '@angular/http';
    
    
                @Injectable()
                export class HttpClientService extends RequestOptions {
    
                    constructor(private requestOptionArgs:RequestOptions) {
                        super();     
                    }
    
                    addHeader(headerName: string, headerValue: string ){
                        (this.requestOptionArgs.headers as Headers).set(headerName, headerValue);
                    }
                }
    

    and when you calling this from another place use this.httpClientService.addHeader("Authorization", "Bearer " + this.tok);

    and you will see the added header eg:- Authorization as follows

提交回复
热议问题