我在调用订阅中的函数时遇到问题。在此函数中,我调用了一个刷新令牌的订阅,如果返回"true“,则调用相同的函数,但此操作不起作用。
主要功能:
getLicenseList():Observable<License[]>{
let licenseList:License[] =[];
return this._http.get("license/list").map(res => {
let processedData = res;
if(processedData['status'] == "401"){
this._http.refreshToken().subscribe(result => {
this.getLicenseList();
});
}else{
for(let license of processedData['data']){
const obj = new License(license['name'],license['start_date'],license['expire_date'], license['type'], license['duration'],license['id'],license['id_user'], license['projectsList']);
licenseList.push(obj);
}
return licenseList;
}
});
}用于刷新令牌的函数:
refreshToken():Observable<boolean>{
let url = 'auth/refresh/self';
return this.post(url, localStorage.getItem('refresh_token')).map(data => {
if(data['access_token'] != null || data['refresh_token'] != null){
window.localStorage.setItem('access_token', data['access_token']);
window.localStorage.setItem('refresh_token', data['refresh_token']);
return true;
}
}).catch(error =>Observable.throw("Expired refresh token"));
}在进入res的调试模式下,我是否可以看到:“input意外结束”
发布于 2018-06-13 21:20:21
您可以使用flapMap合并可观测数据
return http.get(req1)
.flatMap(resp => {
// whatever you need
return this.http.post(req2);
})发布于 2018-06-13 21:46:49
getLicenseList():Observable<License[]>{
let licenseList:License[] =[];
return this._http.get("license/list").map(async res =>{
let processedData = res;
if(processedData['status'] == "401"){
this._http.refreshToken().subscribe(result =>{ if(result){ return this.getLicenseList();} });
else{
for(let license of processedData['data']){
const obj = new License(license['name'],license['start_date'],license['expire_date'], license['type'], license['duration'],license['id'],license['id_user'], license['projectsList']);
licenseList.push(obj);
}
return licenseList;
}
});
}我看到你正在向构造器传递一堆变量。我建议传递整个对象,并让构造函数负责解析。在可观察对象内部使用async和await来让事情等待。除了这个伟大的代码!!
https://stackoverflow.com/questions/50837233
复制相似问题