对于这样的组件

<template>
  <div>
    <router-link :to="{name:'section', params: { sectionId: firstSectionId }}">Start</router-link>
  </div>
</template>
    
<script lang="ts">
  import { mapActions } from "vuex"
    
  export default {
    mounted() {
      this.getSectionId()
    },
    computed: {
      firstSectionId() {
        return this.$store.state.firstSectionId
      }
    },
    methods: mapActions(["getSectionId"])
  }
</script>

store :

const store: any = new Vuex.Store({
    state: {
        firstSectionId: null
    },
    // actions,
    // mutations
})

我在getSectionId操作中有一个web请求,它异步获取数据并调用一个将填充firstSectionId的变异.在初始渲染过程中,firstSectionIdnull,我得到警告,在渲染router-link的过程中,缺少所需的参数.

这里加v-if="firstSectionId"不是问题.但一般来说,从服务器获取要显示的数据的方法是什么?目前,我的所有组件都在判断渲染前存储中是否存在数据,这是正常的还是有更好的方法在渲染前等待加载数据?

推荐答案

异步获取数据的一种方法是在vuex store actions中使用promise.

Vue.http.get(API_URL)
  .then((response) => {
     //use response object      
  })
  .catch((error) => {
    console.log(error.statusText)
  });

证明我向this route提出了请求.您可以看到响应应该是什么样子.让我们将响应对象保存为状态.用户array.

store.js

const store = new Vuex.Store({
  state: {
    users: []
  },  
  mutations: {
    FETCH_USERS(state, users) {
      state.users = users
    }
  },
  actions: {
    fetchUsers({ commit }, { self }) {          
      Vue.http.get("https://jsonplaceholder.typicode.com/users")
        .then((response) => {
          commit("FETCH_USERS", response.body);
          self.filterUsers();   
        })
        .catch((error) => {
          console.log(error.statusText)
        });
    }
  }
})
    
export default store

您注意到提交后有self.filteruser()个方法.这是关键时刻.在此之前,我们是committing a mutation,这是同步操作,我们确信我们将有我们的react 存储.filterUsers()方法(don't forget to pass self parm)中可使用的状态

Users.vue

import store from "../store/store"

export default {
  name: 'users',
  created() {
    this.$store.dispatch("fetchUsers", { self: this })       
  },
  methods:{
    filterUsers() {
      //do something with users
      console.log("Users--->",this.$store.state.users)       
    }
  }
}

Better ways (ES6 & ES7)

ES6 Promises for asynchronous programming

//User.vue
created() {
  this.$store.dispatch("fetchUser").then(() => {
    console.log("This would be printed after dispatch!!")
  })
}

//store.js
actions: {
  fetchUser({ commit }) {
    return new Promise((resolve, reject) => {
      Vue.http.get("https://jsonplaceholder.typicode.com/users")
        .then((response) => {
          commit("FETCH_USERS", response.body);
          resolve();
         })
         .catch((error) => {
           console.log(error.statusText);
         });
    });
  }
}

ES7: async/await

为了摆脱回调地狱,并改进异步编程,请使用async函数,您可以promise 使用await.代码看起来更容易理解(就像它是同步的),但是对于浏览器来说代码是不可读的,所以你需要Babel transpiler来运行它.

actions: {
  async actionA ({ commit }) {
    commit('gotData', await getData())
  },
  async actionB ({ dispatch, commit }) {
    await dispatch('actionA') // wait for actionA to finish
    commit('gotOtherData', await getOtherData())
  }
}

Vue.js相关问答推荐

更新 Shopware 管理 CMS 组件中的编辑器视图

如何以惯用的方式触发 vue3 中同级组件的效果?

Vuetify 3 v-radio-group 将模型设置为 null

Vuetify右侧并排switch

Vue CLI: 如何在一个vtextfield更改时让另一个vtextfield的值也同时更改,但同时又可以覆盖它?

如何将模板传递给EJS菜单

如何向 添加方法和 v-model?

是否可以将 nuxt3 与类星体框架一起使用

Svelte 中的简单react 性实验以意想不到的方式失败了. Vue 等效项没有

如何 for each 动态创建的按钮/文本字段设置唯一变量?

如何 for each 用户分组聊天消息?

如何在 vue 的 scss 中使用深度 Select 器

将数据传递给另一条路由上的组件

如何从 vue.js 中的自定义组件中冒泡点击事件?

如何从 bootstrap-vue 模态监听事件?

在 Vuejs 中计算 DOM 元素之间距离的最佳方法是什么?

Vue路由安全

基于条件的VueJS HTML元素类型

我可以使用react路由创建别名路由吗?

在vue中单击时如何模糊元素