猿问

如何调用返回值是异步请求函数的函数

在vue的methods,函数search作用是返回请求数据,但是报错
search(options){
consturl=this.isTeacher?'/api/search_Teacher':'/apis/org/search_student';
constparams=options.ids?{ids:options.ids}:{keyword:this.keyword};
returnthis.$http.get(url,{params}).then((response)=>{
returnresponse.body;
},(err)=>{
console.log(err);
})
},
但是不明白如何调用这个search,获取到data??尝试下面的方法,报错,this.searchisnotafunction??
this.seach({ids:this.selected}).then((res)=>{
if(!res)this.$message.error('搜索失败');
elsethis.selectedData=res;
});
有没有好的方法,谢谢
ABOUTYOU
浏览 754回答 2
2回答

PIPIONE

asyncsearch(options){consturl=this.isTeacher?'/api/search_Teacher':'/apis/org/search_student';constparams=options.ids?{ids:options.ids}:{keyword:this.keyword};try{constres=awaitthis.$http.get(url,{params})returnres.body}catch(e){console.log(e)}},useconstdata=awaitthis.search({ids:this.selected})if(!data)this.$message.error('搜索失败');elsethis.selectedData=data;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答