初始化文件夹 indedx.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
num: 100
},
mutations: {
addNum (state, val = 1) {
state.num += val
},
subNum (state, val = 1) {
state.num -= val
},
setNum (state, val) {
state.num = val
}
},
actions: {
asyncAddNum (store, val = 1) {
setTimeout(() => {
store.commit('addNum', val)
}, 2000)
},
asyncSubNum (store, val = 1) {
setTimeout(() => {
store.commit('subNum', val)
}, 2000)
}
}
})
export default store
Addltem.vue
<template>
<div>
<h3>AddItem组件</h3>
<p>已知库存数: {{ $store.state.num }}</p>
<button @click="addFn">库存+1</button>
<button @click="asyncAddNumFn">延迟2秒,库存+5</button>
</div>
</template>
<script>
export default {
name: 'AddItem',
methods: {
addFn () {
this.$store.commit('addNum')
},
asyncAddNumFn () {
this.$store.dispatch('asyncAddNum', 5)
}
}
}
</script>
SubItem.vue
<template>
<div>
<h3>SubItem组件</h3>
<p>已知库存数: {{ num }}</p>
<p>{{ age }}</p>
<button @click="subFn">库存-1</button>
<button @click="asyncSubNumFn">延迟2秒,库存-5</button>
</div>
</template>
<script>
import { mapState, mapMutations, mapActions } from 'vuex'
export default {
name: 'SubItem',
computed: {
...mapState(['num', 'age'])
},
methods: {
...mapMutations(['subNum']),
...mapActions(['asyncSubNum']),
subFn () {
this.subNum()
},
asyncSubNumFn () {
this.asyncSubNum(5)
}
}
}
</script>
App.vue
<template>
<div id="app">
<h1>根组件</h1>
<span>库存总数:</span>
<!-- <input type="text" v-model="$store.state.num"> -->
<input type="text" :value="num" @input="inputFn">
<!-- v-model = v-bind + v-on -->
<div style="border:1px solid black; width: 300px;">
<AddItem/>
</div>
<hr>
<div style="border:1px solid black; width: 300px;">
<SubItem/>
</div>
</div>
</template>
<script>
import AddItem from './components/AddItem'
import SubItem from './components/SubItem'
import { mapState, mapMutations } from 'vuex'
export default {
name: 'App',
computed: {
...mapState(['num'])
},
components: {
AddItem,
SubItem
},
methods: {
...mapMutations(['setNum']),
inputFn (e) {
this.setNum(+e.target.value)
}
}
}
</script>
<style>
#app {
width: 300px;
margin: 20px auto;
border:1px solid #ccc;
padding:4px;
}
</style>
语法总结: 使用state :方式方式1: 组件内 - 直接使用
this.$store.state.变量名
方式2: 组件内 - 映射使用 (推荐)
import { mapState } from 'vuex'
export default {
computed: {
...mapState(['state里的变量名'])
}
}
使用mutations :方式方式1: 组件内 - 直接使用
this.$store.commit("mutations里的函数名", 具体值)
方式2: 组件内 - 映射使用 (推荐)
import { mapMutations } from 'vuex'
export default {
methods: {
...mapMutations(['mutations里的函数名'])
}
}
使用actions :方式方式1: 组件内 - 直接使用
this.$store.dispatch('actions函数名', 具体值)
方式2: 组件内 - 映射使用 (推荐)
import { mapActions } from 'vuex'
export default {
methods: {
...mapActions(['actions里的函数名'])
}
}
总结
-
state、getters 都是 映射 在 组件 的 computed 中 -
mutations、actions 都是 映射 在 组件 的 methods 中 -
mutations 和 actions的区别 相同点:本质 都是 函数、 不同点:
- mutations: 唯一 并且 同步 修改 state的
- actions: 异步 修改 state的、但它不能直接改,最终 必须 commit 到 mutations 上
- 第一个形参的区别:mutations 第一个 形参 是
state ,actions 第一个 形参 是 store -
何时需要使用vuex? ? 多个组件需要 共享 数据 的时候,如果组件是 父子关系, 用 父传子 或 子传父
|