Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。
普通的Vue状态自管理在遇到多个视图共同依赖同一状态,或者会改变同一状态时,会遇到难以管理,难以维护的麻烦。为了解决这个麻烦,Vue官方基于全局单例模式提供了Vuex状态管理框架。
在Vuex框架中,Vue视图,状态,数据之间的关系如下图所示:
如果您不打算开发大型单页应用,使用 Vuex 可能是繁琐冗余的。确实是如此——如果您的应用够简单,您最好不要使用 Vuex。一个简单的 store 模式就足够您所需了。但是,如果您需要构建一个中大型单页应用,您很可能会考虑如何更好地在组件外部管理状态,Vuex 将会成为自然而然的选择。
npm i Vuex
如下图所示:
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state:{
},
mutations:{
},
actions:{
},
getters:{
}
})
new Vue({
store,
render: h => h(App),
}).$mount('#app')
更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数.
你不能直接调用一个 mutation handler。这个选项更像是事件注册:“当触发一个类型为 increment 的 mutation 时,调用此函数。”要唤醒一个 mutation handler,你需要以相应的 type 调用 store.commit 方法
Action 类似于 mutation,不同在于:
- Action 提交的是 mutation,而不是直接变更状态。
- Action 可以包含任意异步操作。
- Action的分发使用关键字
const store = new Vuex.Store({
state:{
count:0
},
mutations:{
increment(state,n){
state.count+=n
console.log(state.count)
}
},
actions:{
},
getters:{
}
})
<template>
<div id="app">
{{count}}
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
computed:{
count(){
return this.$store.state.count
}
},
methods:{
}
}
效果如下:
<template>
<div id="app">
{{count}}
<input type="button" @click="handleClick" value="Count++" />
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
computed:{
count(){
return this.$store.state.count
}
},
methods:{
handleClick(){
return this.$store.state.count++;
}
}
}
</script>
效果如下:
mutations:{
increment(state,n){
state.count+=n
console.log(state.count)
}
},
<template>
<div id="app">
{{count}}
<input type="button" name="count2" @click="$store.commit('increment',2)" value="Count+2" />
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
computed:{
count(){
return this.$store.state.count
}
},
methods:{
}
}
</script>
<style>
</style>
const store = new Vuex.Store({
state:{
count:0
},
mutations:{
increment(state,n){
state.count+=n
console.log(state.count)
}
},
actions:{
},
getters:{
doubleCount(state){
return state.count * 2
}
}
})
<template>
<div id="app">
{{$store.getters.doubleCount}}
<input type="button" name="count2" @click="$store.commit('increment',2)" value="Count+2" />
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
computed:{
},
methods:{
}
}
</script>
<style>
</style>
效果如下:
const store = new Vuex.Store({
state:{
count:0
},
mutations:{
increment(state,n){
state.count+=n
console.log(state.count)
}
},
actions:{
increment(state,n){
setTimeout(() => {
state.commit('increment',n)
},1000);
}
},
getters:{
doubleCount(state){
return state.count * 2
}
}
})
<template>
<div id="app">
{{$store.getters.doubleCount}}
<input type="button" name="count1" @click="$store.dispatch('increment',3)" value="Count+3" />
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
computed:{
},
methods:{
}
}
</script>
<style>
</style>
效果如下:
原文:https://www.cnblogs.com/wenpeng/p/12318124.html