官方Vuex文档对于初学者理解起来比较困难,本篇文章参考黑马视频Vuex从入门到实战,旨在使初学者能够快速理解与使用Vuex,有助于加深对官方文档的理解。
Vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。
优点:
什么样的数据适合存储到Vuex中:
一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依旧存储在组件自身的data中即可。
对于如何搭建一个完整的Vue项目,可以查看我的这篇文章:如何快速搭建一个完整的Vue项目
方法一:在 Vue 之后引入 vuex 会进行自动安装:
<script src="/path/to/vue.js"></script>
<script src="/path/to/vuex.js"></script>
方法二: 在项目终端内通过npm或yarn安装:
npm install vuex@next --save
yarn add vuex@next --save
安装 Vuex 之后,创建一个 store,在main.js页面引入store
import Vue from 'vue'
import App from './App.vue'
import store from './store'
Vue.config.productionTip = false
new Vue({
store,
render: h => h(App)
}).$mount('#app')
在store文件夹下index.js页面创建store
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
modules: {
}
})
State提供唯一的公共数据源,所有共享的数据都要统一放到 Store的 State变量中进行存储。
//创建store数据源,提供唯一公共数据
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 0
},
mutations: {
},
actions: {
},
modules: {
}
})
1.通过 this.$store.state.全局数据名称访问
<h3>当前count值为:{{$store.state.count}}</h3>
2.通过刚オ导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的 computed计算属性:
<h3>当前count值为:{{ xCount }}</h3>
//1.导入辅助函数 mapState
import { mapState } from 'vuex'
export default {
data() {
return {}
},
computed: {
// mapState 可以接收数组或对象形式的参数 映射为计算属性,下面分别示例
//2.1 传入数组
...mapState(['count']),
//2.2 对象形式 可以自定义名称
...mapState({
xCount:state => state.count
})
},
}
Mutations用于变更 Store中的数据,只有 mutations里的函数,才有权利修改 state 的数据,mutations里不能包含异步操作。
注意
1.this.$store.commit() 是触发 mutations的第一种方式
Increment() {
this.$store.commit('add')
}
触发 mutations 时传递值
IncrementN(){
this.$store.commit('addN',5)
}
mutations: {
add(state) {
// 变更状态
state.count++
},
addN(state,step){
// 变更状态
state.count += step
}
},
2.触发mutations的第二种方式:
从vuex中按需导入 mapMutations函数 import {mapMutations} from 'vuex'
通过刚才按需导入的 mapMutations函数,映射为当前组件的methods函数。`
// store
mutations: {
add(state) {
// 变更状态
state.count++
},
sub(state) {
state.count--
},
addN(state, step) {
// 变更状态
state.count += step
},
subN(state, step) {
state.count -= step
}
},
// 组件A
import { mapState,mapMutations } from 'vuex'
methods:{
...mapMutations(['sub','subN']),
decrement(){
// 调用
this.sub()
},
decrementN(){
this.subN(5)
}
}
Actions用于处理异步任务,如果通过异步操作变更数据,必须通过 Action,而不能使用Mutation,但是在 Action中还是要通过触发Mutation的方式间接变更数据。
注意: 在Actions 中不能直接修改 state中的数据,要通过 mutations修改。
1.this.$store.dispatch 触发 Actions
// 定义 Action
const store = new Vuex.store({
// ...省略其他代码
mutations: {
// 只有 mutations中的函数才有权利修改 state。
// 不能在 mutations里执行异步操作。
add(state) {
state.count++
}
},
actions: {
// 在Actions 中不能直接修改 state中的数据,要通过 mutations修改。
addAsync(context) {
setTimeout(() => {
context.commit('add')
}, 1000);
}
},
})
组件A
// 触发 Action
methods:{
handle(){
// 触发 actions 的第一种方式
this.$store.dispatch('addAsync')
}
}
2 mapActions 映射为方法
1.从Vuex中按需导入 mapActions 函数。
import {mapActions} from 'vuex'
2.将指定的 actions 函数,映射为当前组件 methods 的方法。
methods:{
...mapActions(['subAsync']),
decrementAsync(){
this.subAsync()
}
}
store.js
actions: {
// 在Actions 中不能直接修改 state中的数据,要通过 mutations修改。
subAsync(context){
setTimeout(() => {
context.commit('sub')
}, 1000);
}
}
//定义 Getter
const store = new Vuex.Store({
state:{
count:0
},
getters: {
showNum(state) {
return '当前数量是【' + state.count + '】'
}
},
})
1.通过 this.$store.getters.名称 访问
this.$store.getters.名称
2.mapGetters 映射为计算属性
import { mapGetters } from 'vuex'
computed:{
...mapGetters(['showNum'])
}
源码地址:https://github.com/15009824714/Vuex-demo