@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule, RouterModule, HttpModule
],
providers: [UserService, AuthGuardService,
{ provide: Http, useClass: BaseHttp }
],
bootstrap: [ AppComponent ]
})
@Injectable()
export class BaseHttp extends Http {
constructor (backend: XHRBackend, options: RequestOptions) {
super(backend, options);
let token = localStorage.getItem(AppConstants.tokenName);
options.headers.set(AppConstants.authHeaderName, token);
}
}
@Injectable()
export class BaseHttp extends Http {
constructor (backend: XHRBackend, options: RequestOptions) {
super(backend, options)
}
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
const token = localStorage.getItem(AppConstants.tokenName)
if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
if (!options) {
options = new RequestOptions({})
}
options.headers.set(AppConstants.authHeaderName, token)
} else {
url.headers.set(AppConstants.authHeaderName, token)
}
return super.request(url, options)
}
}
export declare class Request extends Body {
/**
* Http method with which to perform the request.
*/
method: RequestMethod;
/**
* {@link Headers} instance
*/
headers: Headers;
/** Url of the remote resource */
url: string;
/** Type of the request body **/
private contentType;
/** Enable use credentials */
withCredentials: boolean;
/** Buffer to store the response */
responseType: ResponseContentType;
constructor(requestOptions: RequestArgs);
/**
* Returns the content type enum based on header options.
*/
detectContentType(): ContentType;
/**
* Returns the content type of request's body based on its type.
*/
detectContentTypeFromBody(): ContentType;
/**
* Returns the request's body according to its type. If body is undefined, return
* null.
*/
getBody(): any;
}
@Injectable()
export class AuthRequestOptions extends BaseRequestOptions {
merge(options?: RequestOptionsArgs): RequestOptions {
let newOptions = super.merge(options);
let token = localStorage.getItem(AppConstants.tokenName);
newOptions.headers.set(AppConstants.authHeaderName, token);
return newOptions;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有