VueX:如何在页面加载时调用action.js方法?

时间:2019-07-22 19:28:04

标签: javascript vuex nuxt.js

我是nuxt的超级新手,已经接手了一个大项目。我无法确定其他视图的运行方式,但是我想在页面加载时运行axios,有人可以帮我吗?

该项目的结构方式是,它具有一个存储文件夹(猜测为vuex),并获取getter.js,mutations.js,index.js,state,js和actions.js(如果是,则表示歉意)自然的方法)不确定我的项目是自定义设计模式还是nuxt / vuex的goto方法

到目前为止,我已经尝试过了,

computed: {
  plans: 'accountBilling/fetch',
},

但是我相信这是用于getters.js文件的吗?

我有一个action.js文件,并且想要调用并接收页面加载时fetch的响应?

export default {
  async fetch({ commit }, { account_id }) {
    return await this.$axios.get(`/accounts/billing/plans`)
      .then(response => { commit('setPlans', response.data); response.data })
      .catch(error => { throw error })
  },
}

1 个答案:

答案 0 :(得分:0)

在您的根组件中的mounted钩子中:

mounted() {
  this.$store.dispatch("fetch");
}

然后获取计划:

computed: {
  plans() { return this.$store.state.plans; }
}
相关问题