我有一个组件,首先需要调用POST的服务.然后在同一个组件中我想等到POST完成后,调用另一个获取数据的服务.
如何进行GET调用等待POST调用完成?
在new-version.component.ts中:
private createNewVersion(value) {
...
// create new version, then call on all available versions
// POST call
this._newVersionService.createNewVersion(vnr);
// GET call
this._versionService.getAvailableVersions();
...
}
Run Code Online (Sandbox Code Playgroud)
在new-version.service.ts中:
export class NewVersionService {
response$: Subject<any>;
constructor(private _http: Http) {
this.response$ = new BehaviorSubject<any>(null);
}
public createNewVersion(versionNr) {
this._http.post('http://localhost:8080/services/' + versionNr, null, {
method: 'POST',
})
.subscribe(response => {
this.response$.next(response.status);
},
error => console.error(error));
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想知道是否有一种动态的方式来获取基本网址,以便在http请求中使用?
有没有办法动态获取http://192.123.24.2:8080?
public getAllTickets() {
this._http.get('http://192.123.24.2:8080/services/', {
method: 'GET',
headers: new Headers([
'Accept', 'application/json',
'Content-Type', 'application/json'
])
})
Run Code Online (Sandbox Code Playgroud)
所以,我的请求看起来像:
public getAvailableVersions() {
this._http.get('../services', {
method: 'GET',
headers: new Headers([
'Accept', 'application/json',
'Content-Type', 'application/json'
])
})
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种不必硬编码REST调用URL的方法.或者是具有URL的全局变量的唯一选择?
谢谢!