1.添加Vuex
npm install --save Vuex
2.在main.js中配置
import Vuex from 'vuex'
Vue.use(Vuex)
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
3.案例:通过按钮完成自增效果
//main.js
Vue.use(Vuex)
const store = new Vuex.Store({
state:{
count : 0
},
mutations:{
countUp(state){
state.count ++
}
}
})
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
//html
<h1>{{this.$store.state.count}}</h1>
<button @click="add">点击加1</button>
//js
add(){ this.$store.commit("countUp") },
5.登录信息存储案例
我的github:simple-demo-of-Vuex