asynchronous - Vuex:计算属性返回未定义 - asyc Axios

标签 asynchronous vue.js axios vuex

我对数据库进行了异步 axios 调用,当站点加载时,我将通过一个操作来调度该调用。 (我尝试在 Vue 路由器上的 beforeEnter()、Form.Vue 上的 beforeCreated() 和 Created() 中进行调度)

我有一个计算属性,使用 getter 返回信息。

我遇到的问题是页面加载完成后数据到达那里并返回未定义 - 页面上没有呈现任何内容。

如果我检查我的 Vue DevTools,所有数据都位于 State 中的正确位置。

如何让数据在页面之前完成加载?

// Action

async loadInputs({ state, getters, commit, dispatch }) {    
 if (!state.loading) {
  commit('setLoading', true)

  const inputLists = axios.get('/companyInputLists')
  const inputs = axios.get('/companyInputs')

  commit('setLoading', false)
  commit('loadInputs' , [await inputLists, await inputs])

 }
},

set ({commit}, value) {
  commit('updateValue', value)
},

//突变体

setLoading(state, value) {
  state.loading = value
},

 loadInputs(state, data){
  state.layout = {}
  state.data = {}
  data[0].data.map(list => {
    list['inputs'] = []
    state.layout[list.order] = list

    data[1].data.map(input => {
        if(input.list_id == list.id){
            state.layout[list.order].inputs.push(input)
            state.data[input.label] = ''
        }
     })
   })
 },

updateValue(state, value) {
  state.data[value.type] = value.value
},

//获取者

get(state) {
    console.log(state)
    return state
 },
}

//FORM.VUE

  <span>

    //LIST TEST and v-if test
    <div v-if="lists">
      {{lists}}
    </div>
    test
    {{ lists }}



<v-layout row wrap justify-center>

  <draggable class="dragArea layout row wrap justify-center" :options="{group:'lists'}">
    <v-flex v-for="list in lists" v-bind:key="list.index" :class="[list.label, flexBoxSize(list.size)]">

      <v-subheader v-text="list.label"></v-subheader>

      <draggable class="dragArea layout row wrap" :options="{group:'inputs'}">
        <v-flex v-for="item in list.inputs" v-bind:key="item.index" :class="[item.label, flexBoxSize(item.size)]">

          <textfield v-if="item.type_id == 1" formType="clientForm" :label="item.label" :required="item.required"></textfield>
          <email v-if="item.type_id == 2" formType="clientForm" :label="item.label"  :required="item.required"></email>
          <phone v-if="item.type_id == 3" formType="clientForm" :label="item.label"  :required="item.required"></phone>
          <calendar v-if="item.type_id == 4" formType="clientForm" :label="item.label"  :required="item.required"></calendar>
          <googleMap v-if="item.type_id == 5" formType="clientForm" :label="item.label"  :required="item.required"></googleMap>
          <autocomplete v-if="item.type_id == 6" formType="clientForm" :label="item.label"  :required="item.required"></autocomplete>


        </v-flex>
      </draggable>

    </v-flex>
  </draggable>


  <v-layout row wrap justify-center>
    <submitButton formType="clientForm" path="/clientForm" :references="this.$refs"></submitButton>
    <clearButton formType="clientForm" :references="this.$refs"></clearButton>
  </v-layout>

  </v-layout>
 </span>
</template>

<script>
export default {
  components: {
    draggable,
  },

  beforeCreate(){
    this.$store.dispatch('clientForm/loadInputs')
  },

  computed: {
    lists: {
      get() {
        return this.$store.getters['clientForm/get'].layout
      },
      set(value) {
        this.$store.commit('clientForm/updateInputList', value)
      }
    }
  },

Vuex Dev Tools Showing Data in State After Page Loads

最佳答案

我在去年寒假期间找到了答案,并意识到这里从来没有发布过明确的结论。经过多次尝试和错误并阅读文档后,我在 Vue.js 文档中找到了答案。

https://v2.vuejs.org/v2/api/#Vue-set

Vue.set(目标、键、值) 向响应式对象添加属性,确保新属性也是响应式的,从而触发 View 更新。这必须用于向响应式对象添加新属性,因为 Vue 无法检测正常的属性添加(例如 this.myObject.newProperty = 'hi')。

使用此函数,我能够通过 axios 调用加载数据,并让 Vue 检测更改并更新 DOM。

此外,您可能需要注意 Vue.delete 用于删除具有反应性的数据。

关于asynchronous - Vuex:计算属性返回未定义 - asyc Axios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47537868/

相关文章:

javascript - axios 授权 header 仅适用于获取请求

javascript - node.js 异步 waterfall 调用以前的方法

javascript - Node : syncronise pieces of code

javascript - Node JS 同步工作流程与异步请求

vue.js - Vue + Webpack : exclude config. js 被打包工作,但它不加载

vue.js - 如何从vue功能组件提供传递注入(inject)

vue.js - Nuxt + Vuetify。如何应用主题颜色

javascript - 有人可以就我的 Axios Post 请求提供建议吗?

c# - 可观察 (Rx) 用于存储库中的连续(异步)对象流

javascript - 来自浏览器的带有正文的异步 GET 请求