我需要使用类型脚本将函数的返回值分配给局部变量。下面,我已清楚解释它:如何将函数的返回值赋给局部变量?
getdetail1(store){
let Cust_id=this.sharedata.latus_lead.m_type
let url="http:domain.com"
console.log(url);
let res;
this.loginservice.leaddata = null;
this.loginservice.getdetails(url).then(data=>{
let lead=data;
this.details=lead[0];
res=lead[0];
});
return res;
}
并调用它像这样:
let res = this.getdetail1(store);
看,这是我的登录服务代码
getdetails(query){
if (this.leaddata) {
// already loaded data
console.log("already data loaded lead") ;
return Promise.resolve(this.leaddata);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular HTTP provider to request the data,
// then on the response, it'll map the JSON data to a parsed JS object.
// Next, we process the data and resolve the promise with the new data.
this.http.get(query)
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.leaddata = data;
resolve(this.leaddata);
});
});
}
这里我处理答应
您正在使用的承诺? –
http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call –
雅我在这个代码中使用 –