我使用ngx-restangular对角4. http请求我必须建立一个自定义标题,在一些情况下采取从用户和令牌创建将被传递到API中的Requestheader
后一些输入。
这是我所用于设置自定义标题的代码。
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { RestangularModule, Restangular, RestangularHttp } from 'ngx-restangular';
import { RestangularConfigFactory } from '../app.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from '../app.component';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Http, Headers, Response, RequestOptions, } from '@angular/http';
@Component({
templateUrl: 'login.component.html'
})
export class LoginComponent implements OnInit {
model: any = {};
loading = false;
returnUrl: string;
headers: Headers;
options: RequestOptions;
constructor(
private route: ActivatedRoute,
private router: Router,
private restangular: Restangular) { }
ngOnInit() {
// reset login status
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
login() {
this.loading = true;
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
this.headers.append('Authorization', 'Basic ' + btoa(this.model.username + ':' + this.model.password));
this.options = new RequestOptions({ headers: this.headers });
console.log(this.options);
this.restangular.one('authentication').one('user').get([''], [this.headers]).subscribe( response => {
console.log(response);
});
}
}
在下面你所需要的图像图像可以在Request Header
看到一个名为authorization
新的领域有哪些我想用ngx-restangular
来实现。
任何帮助表示赞赏。
使用拦截器=>
RestangularProvider.addFullRequestInterceptor((element, operation, path, url,
headers, params)=> {
return {
params: Object.assign({}, params, {sort:"name"}),
headers: headers,
element: element
}
});
https://github.com/2muchcoffeecom/ngx-restangular#addfullrequestinterceptor