vue.js - 如何使用 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 组件(包含存储的那个).dispatch()
的钩子中使用 Axios REST API 抓取功能:created()
new Vue({
store,
render: (h) => h("frame", [h(App)]),
created() {
this.$store.dispatch('myAxiosFetchFunction');
},
}).$start();
推荐阅读
- google-chrome - 清除 CORS 的浏览器缓存
- python - Python:使用不完整或没有内部网格的 cv2 检测表
- css - 使列占用缺失列的空间
- netlogo - Netlogo中病毒传播过程中将“自我”从易感染者更改为错误
- php - 自 WooCommerce 3+ 起以编程方式创建优惠券
- git - 是否可以悄悄地对 git 存储库进行更改?
- java - 如何使用 Jacob 声明变量以将输出作为 Sub 参数
- python-3.x - 检查一个图像是否是从另一个图像中剪切的
- sendmail - 如何告诉 sendmail 在 dkim 之前进行伪装?
- mysql - 我需要找到一种方法来优化从多个数据库表中提取 laravel 中的数据