请检查下面我的代码。我正在尝试将异步等待实现到vuex中。一切正常,但是我想在此操作之后调用另一个操作,因此我尝试使用async await,但是它不起作用。在控制台中console.log(“ after all all” + res)res变量显示未定义。

action.js

import { createUpload } from '../api/index'
export default {
    fetchImageUrl: async({ commit }, reader) => {
      let res = await createUpload({ commit }, reader);
      console.log("after all this" +res)
    }
}


api / index.js

import { feathersClient } from './apiClient'
const uploadService = feathersClient.service('uploads');
export const createUpload = ({commit}, reader) => {
  uploadService
    .create({uri: reader.result})
    .then(function(response){
        commit('setImageUrl', { url: response.imageurl })
        return true;
    });
}


突变.js

export default {
    setImageUrl: (state,{ url } ) => {
      state.imageUrl = url
    }
}


LeftPanel.vue

const reader  = new FileReader();
export default {
  name: 'left-panel',
  data () {
    return {
      open: true
    }
  },
  methods: {
    uploadFile: function (event) {
      let store = this.$store;
      let file = event.target.files[0];
      reader.readAsDataURL(file);
      reader.onload = function(event) {
        return store.dispatch('fetchImageUrl',reader)
      };
    }
  },
  components: {
    'add-text': AddText
  },
}

最佳答案

return之前添加uploadService.create语句。

index.js

export const createUpload = ({commit}, reader) => {
  return uploadService
    .create({uri: reader.result})
    ...
}

09-18 08:18