1.vuex介绍
1.1 vuex是什么?
vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间的数据共享。
一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依旧存储在组件自身的data中即可。
①能够在vuex中集中管理共享的数据,易于开发和后期维护
②能够高效地实现组件之间的数据共享,提高开发效率
③存储在vuex中的数据都是响应式的,能够实时保持数据与页面的同步
1.2 安装vuex
- 在 Vue 之后引入 vuex 会进行自动安装:
<script src="/path/to/vue.js"></script>
<script src="/path/to/vuex.js"></script>
- NPM
npm install vuex --save
- Yarn
yarn add vuex
- 在一个模块化的打包系统中,您必须显式地通过 Vue.use() 来安装 Vuex:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
1.3 基本使用
- 安装vuex依赖包
npm install vuex --save
- 导入vuex包
import Vuex from 'vuex
Vue.use(Vuex)
- 创建store对象
const store = new Vuex.Store({
// state中存放的就是全局共享的数据
state:{count:0}
})
- 将store对象挂载到vue实例中
new Vue({
render: h => h(App),
store
}).$mount('#app')
2. 核心概念
2.1 State
定义state对象
const store = new Vuex.Store({
// state中存放的就是全局共享的数据
state:{count:0}
})
State提供唯一的公共数据源,所有共享的数据都要统一放到Store的 State中进行存储。
组件访问state中的数据
- 第一种方式:
this.$store.state.count
- 第二种方式:按需导入mapstate函数
通过刚才导入的mapState函数,将当前组件需要的全局数据,映射为当前组件的computed计算属性://从 vuex中按需导入mapstate函数 import { mapstate } from 'vuex'
// 2.将全局数据,映射为当前组件的计算属性 computed:{ ...mapState(['count']) }
2.2 Mutation
this.$store.state.count直接修改数据不合适,不允许组件直接修改store的数据
所以用mutation修改store的数据,通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化。
不能处理异步操作,异步操作用action
2.2.1 mutation无参修改
定义mutation对象
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
add(state) {
// 变更状态
state.count++
}
}
})
触发mutation
methods:{
handle1(){
this.$store.commit('add')
}
}
2.2.2 有参修改
定义mutation对象
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
addN(state,step) {
// 变更状态
state.count+= step
}
}
})
触发mutation
methods:{
handle2(){
this.$store.commit('addN',3)
}
}
2.2.3 按需导入mapMutations函数
this.$store.commit()是触发mutations的第一种方式,触发mutations的第二种方式:
import {mapMutations} from 'vuex'
通过刚才导入的mapMutations函数,将需要的mutations函数,映射为当前组件的methods方法:
main.js部分
mutations: {
sub(state){
state.count +=1
},
subN(state,step){
state.count += step
}
}
组件部分:
methods: {
...mapMutations(["sub","subN"]),
mapMutation1() {
this.sub();
},
mapMutation2(){
this.subN(10)
}
},
或者
<button @click="sub">sub</button>
<button @click="subN(100)">subN</button>
methods: {
...mapMutations(["sub","subN"]),
}
## 2.3 Action
Action用于处理异步操作
如果通过异步操作变更数据,必须通过Action,而不能使用Mutation,但是在Action中还是要通过触发Mutation的方式间接变更数据。
Action 提交的是 mutation,而不是直接变更状态。
Action 可以包含任意异步操作。
### 2.3.1 触发Action的第一种方式:this.$store.dispatch()
main.js部分
```js
mutations:{
add(state){
state.count1++
},
addN(state,step){
state.count1+=step
},
},
actions:{
incrementAsync2(context){
setTimeout(()=>{
context.commit('add')
},1000)
},
incrementAsync3(context,step){
setTimeout(()=>{
context.commit('addN',step)
},1000)
}
}
组件部分:
methods:{
action2() {
this.$store.dispatch("incrementAsync2");
},
action3() {
this.$store.dispatch("incrementAsync3",10);
},
}
2.3.2 触发Actionde的第二种方式:mapActions
main.js部分不变
组件部分:
import { mapActions } from "vuex";
methods: {
...mapActions(["incrementAsync2", "incrementAsync3"]),
mapaction1() {
this.incrementAsync2()
},
mapaction2() {
this.incrementAsync3(10)
},
},
或者:
<button @click="incrementAsync2">mapaction1</button>
<button @click="incrementAsync3(100)">mapaction1</button>
methods: {
...mapActions(["incrementAsync2", "incrementAsync3"]),
}
2.4 Getter
Getter用于对Store 中的数据进行加工处理形成新的数据。
Getter可以对Store 中已有的数据加工处理之后形成新的数据,类似Vue的计算属性。
Store 中数据发生变化,Getter的数据也会跟着变化。
定义getters
getters: {
showNum: state => {
return '当前最新的数量是【' + state.count + '】'
}
}
使用getters的第一种方法:
this.$store.getters.名称
使用getters的第二种方法:
import {mapGetters} from "vuex";
export default {
computed: {
...mapGetters(["showNum"]),
},
};