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

在vue的methods,函数search作用是返回请求数据,但是报错

search(options) {

  const url = this.isTeacher? '/api/search_Teacher' : '/apis/org/search_student';

  const params = options.ids ? { ids: options.ids } : { keyword: this.keyword };

 return this.$http.get(url, { params }).then((response) => {

   return response.body;

  }, (err) =>{

   console.log(err);

  })

},

但是不明白如何调用这个search,获取到data??
尝试下面的方法,报错,this.search is not a function??

  this.seach({ ids: this.selected }).then((res) => {

    if (!res) this.$message.error('搜索失败');

    else this.selectedData = res;

  });

有没有好的方法,谢谢

不负相思意
浏览 454回答 1
1回答

www说

async search(options) {  const url = this.isTeacher? '/api/search_Teacher' : '/apis/org/search_student';  const params = options.ids ? { ids: options.ids } : { keyword: this.keyword };  try {    const res = await this.$http.get(url, { params })    return res.body  } catch (e) {    console.log(e)  }},useconst data = await this.search({ ids: this.selected })if (!data) this.$message.error('搜索失败');else this.selectedData = data;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript