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.案例:通过按钮完成自增效果
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/>'
})
<h1
>{{this.$store
.state
.count
}}</h1
>
<button @click
="add">点击加
1</button
>
add(){ this.$store
.commit("countUp") },
5.登录信息存储案例
我的github:simple-demo-of-Vuex
转载请注明原文地址:https://ipadbbs.8miu.com/read-54152.html