首页 > 解决方案 > 如何使用 rest API 在应用程序启动时正确初始化 Vuex 商店?

问题描述

我想使用从 API 获取的一些数据来初始化商店(我正在使用 axios) 我怎样才能在应用程序启动时做到这一点,只有一次?

我有一个导出我的商店的 store.js 文件和一个执行此操作的 main.js 文件:

import Vue from "nativescript-vue";
import store from "./store";

new Vue({
  store,
  render: (h) => h("frame", [h(App)]),
}).$start();

标签: vue.jsaxiosnativescriptvuexnativescript-vue

解决方案


尝试在主 Vue 组件(包含存储的那个).dispatch()的钩子中使用 Axios REST API 抓取功能:created()

new Vue({
  store,
  render: (h) => h("frame", [h(App)]),
  created() {
    this.$store.dispatch('myAxiosFetchFunction');
  },
}).$start();

推荐阅读