vuex的使用
一、vuex介绍
vuex是一个专门为vue.js设计的集中式状态管理架构。在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。简单的说就是data中需要共用的属性。使用vuex来实现数据共享。
二、简单使用
1. 引入vuex
(1)安装:npm install vuex --save
注: 加--save,会在package.json中的“dependencies”生成,因为你这个包在生产环境中是要使用的。不添加会保存到"devDependencies"开发环境中。
(2)新建一个vuex/store.js 或 store/index.js文件,在store/index.js或vuex/store.js中添加
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { //存放改变state的数值的方法 increment (state) { state.count++ } } }) export default store; //用export default 封装代码,让外部可以引用。
(3)使用我们vuex,引入文件。
import store from './store'; //store为(2)自定义的文件 new Vue({ el: '#app', router, store, template: '<App/>', components: { App } })
(4)vue模板中
调用属性:<h3>{{$store.state.count}}</h3>
调用方法:<button @click="$store.commit('increment')">+</button>
三、state访问状态对象
将状态对象赋值给内部对象,也就是把stroe.js中的值,赋值给我们模板里data中的值。我们有四种赋值方式:
1. 直接调用
{{$store.state.count}} 太繁琐,了解不用
2. 通过computed的计算属性直接赋值
computed属性可以在输出前,对data中的值进行改变,我们就利用这种特性把store.js中的state值赋值给我们模板中的data值。
computed:{
count(){
return this.$store.state.count;
}
}
调用属性:{{count}} 比较麻烦,不常用
3. 通过mapState的对象来赋值
import {mapState} from 'vuex';
computed:mapState({
count:state=>state.count
})
使用ES6的箭头函数来给count赋值。
调用属性:{{count}}
4. 通过mapState的数组来赋值
import {mapState} from 'vuex';
computed:mapState(["count"])
调用属性:{{count}} 最简单,在实际项目开发当中常用。
四、Mutations修改状态
1. 最简单的例子
<button @click="$store.commit('add')">+</button>
<button @click="$store.commit('reduce')">-</button>
store.js文件:
const mutations={
add(state){
state.count++;
},
reduce(state){
state.count--;
}
}
2. 传值
const mutations={
add(state,n){
state.count+=n;
},
reduce(state){
state.count--;
}
}
<button @click="$store.commit('add',10)">+</button>
<button @click="$store.commit('reduce')">-</button>
3. 模板获取Mutations方法
import { mapState,mapMutations } from 'vuex';
methods:mapMutations([
'add','reduce'
])
<button @click="reduce">-</button> 可以在模板中直接使用我们的reduce或者add方法了。
五、getters计算过滤操作
getters:在获取数据之前进行的一种再编辑,相当于对数据的一个过滤和加工。可以把它看作store.js的计算属性。
1. getters基本用法:
我们首先要在store.js里声明getters属性。
getters : {
count:function(state){
return state.count +=100; //比如我们现在要对store.js文件中的count进行一个计算属性的操作,就是在它输出前,给它加上100.
}
}
在模板页对computed进行配置。使用ES6中的展开运算符”…”。
computed:{
...mapState(["count"]),
count(){
return this.$store.getters.count;
}
},
注:你写了这个配置后,在每次count 的值发生变化的时候,都会进行加100的操作。
2. 用mapGetters简化模板写法:
import { mapState,mapMutations,mapGetters } from 'vuex';
在computed属性中加入mapGetters
...mapGetters(["count"])
六、actions异步修改状态
actions和之前讲的Mutations功能基本一样,不同点是,actions是异步的改变state状态,而Mutations是同步改变状态。
Action 通过 store.dispatch 方法触发:this.$store.dispatch('increment') / this.$store.dispatch('increment', {amount: 10})
1. 使用:
(1)在store.js中声明actions
在actions里调用add和reduce两个方法。
actions : {
addAction(context){
context.commit('add',10)
},
reduceAction({commit}){
commit('reduce')
}
}
在actions里写了两个方法addAction和reduceAction,在方法体里,我们都用commit调用了Mutations里边的方法。细心的小伙伴会发现这两个方法传递的参数也不一样。
context:上下文对象,这里你可以理解称store本身。
{commit}:直接把commit对象传递过来,可以让方法体逻辑和代码更清晰明了。
(2)模板中的使用
<button @click="addAction">+</button>
<button @click="reduceAction">-</button>
import { mapActions } from 'vuex';
methods:{
...mapMutations([
'add','reduce'
]),
...mapActions(['addAction','reduceAction'])
}
2. 增加异步检验
增加一个计时器(setTimeOut)延迟执行。在addAction里使用setTimeOut进行延迟执行。
setTimeOut(()=>{context.commit(reduce)},3000);
console.log('我比reduce提前执行');
我们可以看到在控制台先打印出了‘我比reduce提前执行’这句话。
七、module模块组
module:状态管理器的模块组操作。
Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter。
1. 使用
(1)声明模块组:
在vuex/store.js中声明模块组,我们还是用我们的const常量的方法声明模块组。代码如下:
const moduleA={
state,mutations,getters,actions
}
声明好后,我们需要修改原来 Vuex.Stroe里的值:
export default new Vuex.Store({
modules:{a:moduleA}
})
(2)在模板中使用
<h3>{{$store.state.a.count}}</h3>
如果想用简单的方法引入,还是要在我们的计算属性中rutrun我们的状态。写法如下:
computed:{
count(){
return this.$store.state.a.count;
}
}

浙公网安备 33010602011771号