所以基本上我需要为所有引用API的请求添加自定义auth标头。在构造函数中,我想添加此标头,然后在类方法中使用this.http
import { Injectable } from '@angular/core';
import { Config, Events } from 'ionic-angular';
import { Http } from '@angular/http';
@Injectable()
export class APIRequest {
constructor (
private http: Http,
private config: Config,
) {
this.http.headers.append('My-Custom-Header','MyCustomHeaderValue');
}
}
答案 0 :(得分:2)
我以这种方式对标题使用常见功能
let method = 'POST';
let requestOptions: RequestOptions = new RequestOptions({
headers: this.jsonHeaders(),
method: method
});
jsonHeaders()
//功能
public jsonHeaders(): Headers {
let headers: Headers = new Headers();
headers.append('Content-Type', 'application/json; charset=utf-8');
headers.append("Cache-Control", "no-cache");
headers.append("Cache-Control", "no-store");
headers.append("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
if(this.token) {
headers.append('Authorization', 'Bearer ' + this.token);
}
return headers;
}
HTTP Request
let url = '/login'
this.http.request(url, requestOptions)