问题:在vue2中使用vuex
vuex报错:Property or method “$store“ is not defined on the instance but referenced during render.
**解决方法:**
vuex的版本不对,获取不到store。vue3只能用vuex4版本,vue2只能用vuex3版本。
2022年2月7日之后,vue3成了默认版本,vuex4相应的也成了默认版本,vuex4只能在vue3中使用。所以对于vue2,就不能默认下载最新版本了,要
**npm install vuex@3 -- save**
注意⚠️:
原因:
Vue 2.x和Vuex 4.x版本不对应
Vue 3 匹配 Vuex 4 ,Vue 2 匹配 Vuex 3
解决办法:
卸载原来安装的vuex4
npm uninstall vuex --save
安装vuex3
npm install vuex@3 --save
如果报错: **Cannot read property 'state' of undefined"**
在mian.js 中,创建实例时,store应该小写,也可以写成store:Store
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})