Vue中搭建Vuex环境的具体操作步骤是什么
Admin 2022-07-07 群英技术资讯 1197 次浏览
今天就跟大家聊聊有关“Vue中搭建Vuex环境的具体操作步骤是什么”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“Vue中搭建Vuex环境的具体操作步骤是什么”文章能对大家有帮助。在src目录下创建一个文件夹store,在store文件夹内创建一个index.js文件
index.js用于创建Vuex中最核心的store
// scr/vuex/index.js
// 引入Vuex
import Vuex from 'vuex'
// 用于响应组件中的动作
const actions = {}
// 用于操作数据
const mutations = {}
// 用于存储数据
const state = {}
// 创建store
const store = new Vuex.Store({
actions,
mutations,
state
})
// 导出store
export default store
// main.js
import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
import store from './store/index'
Vue.use(Vuex)
new Vue({
render:h => h(App),
store
}).$mount('#app')
但是这样会出现报错:
[vuex] must call Vue.use(Vuex) before creating a store instance
意思为:[vuex] 在创建 store 实例之前必须调用 Vue.use(Vuex)
原因:在我们导入store的时候,先执行引入文件的代码,所以在执行以下代码时,引入的文件已经被执行了
既然这样子,那么我们交换import store from './store/index',Vue.use(Vuex)两行代码
可是实际的结果是:[vuex] must call Vue.use(Vuex) before creating a store instance,依旧报错
原因:这是脚手架解析import语句的问题,会将import引入的文件提前,放在代码的最开始,也是最开始解析,然后解析本文件的代码
正确的写法:
// scr/store/index.js
// 引入Vuex和Vue
import Vuex from 'vuex'
import Vue from 'vue'
// 应用Vuex插件
Vue.use(Vuex)
// 用于响应组件中的动作
const actions = {}
// 用于操作数据
const mutations = {}
// 用于存储数据
const state = {}
// 创建store
const store = new Vuex.Store({
actions,
mutations,
state
})
// 导出store
export default store
// main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store/index'
new Vue({
render:h => h(App),
store
}).$mount('#app')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
jquery模块化是什么?为什么我们应该使用jquery模块化来开发项目,下面让作者来介绍一下。 我们都知道jquery使用非常的简单,比起javascript来说,不管是jquery函数,还是jquery选择器,都是javascript无法可比拟的,我们可能只是少量的使用一些javascript事
在数组的使用中我们通常会有数组的过滤的需求,比如值在某个范围内的数字,用来组成一个新的数组,这就用到了数组的过滤,下面这篇文章主要给大家介绍了关于JS数组中filter方法使用的相关资料,需要的朋友可以参考下
JavaScript中关系运算符有几种?JavaScript关系运算符有==、===、!=、!==、<、>等等,这些符号都表示什么意思呢?用法和使用注意事项是什么?这篇文章小编就给大家来简单的介绍一下,有需要的朋友也可以参考,接下来就跟随小编一起了解看看吧。。
本文实例为大家分享了vue实现拖拽交换位置的具体代码,供大家参考,具体内容如下template div class=root transition-group tag=div class=container div class=item :class=item + i
本文实例为大家分享了JavaScript实现九宫格拖拽效果的具体代码,供大家参考,具体内容如下关于一些拼图游戏什么的,见人家效果做的不错,参考下别人写的代码,我也尝试着做了个。!DOCTYPE htmlhtml head meta charset=UTF-8 title/title
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008