我有这个错误,还没有解决它,虽然我在MDN和这里研究了很多.正如标题所说,我正在try 使用async和await,但js并没有等待'await'函数结束.这是:

 methods: {
    async search (terms, done) {
      console.log('1.')
      this.filter = this.$refs.chipsInput.input
      await this.loadtags()
      console.log('3.')
      done(this.tagsList)
    },
    loadtags () {
      this.$axios
        .get('/api/tags/?id__icontains=&id=&name__icontains=' + this.filter + '&name=&ordering=name&page_size=20')
        .then(response => {
          console.log('2.', response.data.results)
          let temp = response.data.results
          this.tagsList = temp.map(obj => {
            return {
              name: obj.name,
              label: obj.name,
              value: obj.name,
              idField: obj.id
            }
          })
        })
    },
I am not able to post pictures yet, but add a link where you can look the console log where js prints the '3.' (which is placed after the await call) before '2.': Image: console

我做错了什么?已经try 过这样修改等待:

推荐答案

您没有从loadtags方法返回任何内容,因此代码不会等待.

改变这一点:

loadtags () {
  this.$axios
    .get(...

为此:

loadtags () {
  return this.$axios
    .get(...

async/await或多或少只是promise 上的甜言蜜语,所以用另一种方法来回报promise 会让你有所期待.

Vue.js相关问答推荐

正在try 访问vutify中v-select(ItemProps)的嵌套json值

如何将外部 svg 转换为 Vue3 组件?

我如何使用 vuejs 在我的 url 中编码一个令牌

Vue,Composition API:如何从react 式转换中跳过深度嵌套的属性?

如果在 VueJS/VuetifyJS 中切换switch,则调用函数

在 VueJS 中为 Get、Post、Patch 配置全局标头的最佳方法

在重复表行中

如何使用 v-on:change 将输入文本传递给我的 vue 方法?

Vuetify 置顶工具栏

Vue 两种方式的 prop 绑定

自定义props类型

如何判断 Vue 组件是否处于活动状态

VueJS 使用 prop 作为数据属性值

Laravel 事件未广播

Vue router路由,Root Vue 没有数据?

如何克隆props对象并使其无react

为 Vue 组件动态添加属性

基本 vue.js 2 和 vue-resource http 获取变量赋值

如何从 vue-apollo 中访问 this.$route?

Vue.js 无法读取 null 的属性 length长度