在vue3.x项目中需使用vuex4.x版本;
安装
直接下载/CDN
https://unpkg.com/vuex
npm /cnpm /yarn
npm install vuex@next --save
cnpm install vuex@next --save
yarn add vuex@next --save
项目中使用
在根目录下创建store文件夹,在store文件夹下创建index.js文件,在index.js文件中书写代码,与vue2.0中使用的vuex相差不大,直接上代码:
import { createStore } from 'vuex'
export default createStore({
state: {
count: 0
},
mutations: {
// 进行数据更新,改变数据状态
countType(state, action){
state.count = state.count + action.payload
}
},
actions: {
//执行动作,数据请求
addCount({commit}){
fetch('../data.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {