vuex的学习笔记

时间:2023-03-09 22:47:37
vuex的学习笔记

什么是Vuex?

vuex是一个专门为vue.js设计的集中式状态管理架构。状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。简单的说就是data中需要共用的属性。

引入Vuex(前提是已经用Vue脚手架工具构建好项目)

1、利用npm包管理工具,进行安装 vuex。在控制命令行中输入下边的命令就可以了。

    npm install vuex --save

要注意的是这里一定要加上 –save,因为你这个包我们在生产环境中是要使用的。

2、新建一个store文件夹(这个不是必须的),并在文件夹下新建store.js文件,文件中引入我们的vue和vuex。

    import Vue from 'vue';
import Vuex from 'vuex';

3、使用我们vuex,引入之后用Vue.use进行引用。

    Vue.use(Vuex);

通过这三步的操作,vuex就算引用成功了。

4、在main.js 中引入新建的vuex文件

  import store from './vuex/store'

5、再然后 , 在实例化 Vue对象时加入 store 对象 :

new Vue({
router:router,
store,
   el: '#app',
render: h => h(App)
})

Demo

1、现在我们store.js文件里增加一个常量对象。

Vue.use(Vuex)

const state = {
notes: [],
activeNote: {},
count:1
}

2、用export default 封装代码,让外部可以引用。

export default new Vuex.Store({
state
})

3、新建一个vue的模板

 <template>
<div>
<h2>{{msg}}</h2>
<hr/>
<h3>{{$store.state.count}}</h3>
</div>
</template>
<script>
import store from './vuex/store'
export default{
data(){
return{
msg:'Hello Vuex', }
} }
</script>

4、在store.js文件中加入两个改变state的方法。

onst mutations={
add(state){
state.count+=1;
},
reduce(state){
state.count-=1;
}
}

  这里的mutations是固定的写法,意思是改变的,所以你先不用着急,只知道我们要改变state的数值的方法,必须写在mutations里就可以了。

5、在count.vue模板中加入两个按钮,并调用mutations中的方法。

<div>
<button @click="$store.commit('add')">+</button>
<button @click="$store.commit('reduce')">-</button>
</div>

  

一、 通过computed的计算属性直接赋值

computed属性可以在输出前,对data中的值进行改变,我们就利用这种特性把store.js中的state值赋值给我们模板中的data值。

computed:{
count(){
return this.$store.state.count;
}
}

  现在store.js文件里给add方法加上一个参数n。

  const mutations={
add(state,n){
state.count+=n;
},
reduce(state){
state.count-=1;
}
}

  

 <p>
<button @click="$store.commit('add',10)">+</button>
<button @click="$store.commit('reduce')">-</button>
</p>

  在Count.vue里修改按钮的commit( )方法传递的参数,我们传递10,意思就是每次加10.

二、通过mapState的对象来赋值

  import {mapState} from 'vuex';

  

 computed:mapState({
count:state=>state.count //理解为传入state对象,修改state.count属性
})

  

三、通过mapState的数组来赋值

 computed:mapState(["count"])

  

模板获取Mutations方法

实际开发中我们也不喜欢看到$store.commit( )这样的方法出现,我们希望跟调用模板里的方法一样调用。 
例如:@click=”reduce” 就和没引用vuex插件一样。要达到这种写法,只需要简单的两部就可以了:

  import { mapState,mapMutations } from 'vuex';

   methods:mapMutations([
'add','reduce'
]),
或者
methods:{
...mapMutations([
'add','reduce'
]),
}
button @click="reduce">-</button>

  ...三点解构赋值需要babel转义,参考http://www.cnblogs.com/yiyi17/p/7762408.html

getters计算过滤操作

比如我们现在要对store.js文件中的count进行一个计算属性的操作,就是在它输出前,给它加上100.我们首先要在store.js里用const声明我们的getters属性。

 const getters = {
count:function(state){
return state.count +=100;
}
}

  写好了gettters之后,我们还需要在Vuex.Store()里引入,由于之前我们已经引入了state和mutations,所以引入里有三个引入属性。代码如下,

  export default new Vuex.Store({
state,mutations,getters
})

  在store.js里的配置算是完成了,我们需要到模板页对computed进行配置。在vue 的构造器里边只能有一个computed属性,如果你写多个,只有最后一个computed属性可用,所以要对上节课写的computed属性进行一个改造

computed:{
count(){
return this.$store.getters.count;
}
},

  你写了这个配置后,在每次count 的值发生变化的时候,都会进行加100的操作。

actions异步修改状态

actions和之前讲的Mutations功能基本一样,不同点是,actions是异步的改变state状态,而Mutations是同步改变状态。至于什么是异步什么是同步这里我就不做太多解释了,如果你不懂自己去百度查一下吧。

在store.js中声明actions

 const actions ={
addAction(context){
context.commit('add',10)
},
reduceAction({commit}){
commit('reduce')
}
}

  

在actions里写了两个方法addAction和reduceAction,在方法体里,我们都用commit调用了Mutations里边的方法。细心的小伙伴会发现这两个方法传递的参数也不一样。

  • ontext:上下文对象,这里你可以理解称store本身。
  • {commit}:直接把commit对象传递过来,可以让方法体逻辑和代码更清晰明了。

模板中的使用

我们需要在count.vue模板中编写代码,让actions生效。我们先复制两个以前有的按钮,并改成我们的actions里的方法名,分别是:addAction和reduceAction。

<button @click="$store._actions.addAction">+</button>
<button @click="$store._actions.reduceAction">-</button>

  或者

 <p>
<button @click="addAction">+</button>
<button @click="reduceAction">-</button>
</p> methods:{
...mapMutations([
'add','reduce'
]),
...mapActions(['addAction','reduceAction'])
},

  

增加异步检验

  setTimeOut(()=>{context.commit(reduce)},3000);
console.log('我比reduce提前执行');

  

module模块组

随着项目的复杂性增加,我们共享的状态越来越多,这时候我们就需要把我们状态的各种操作进行一个分组,分组后再进行按组编写。那今天我们就学习一下module:状态管理器的模块组操作。

声明模块组:

在vuex/store.js中声明模块组,我们还是用我们的const常量的方法声明模块组。代码如下:

 const moduleA={
state,mutations,getters,actions
}

 声明好后,我们需要修改原来 Vuex.Stroe里的值:.

 export default new Vuex.Store({
modules:{a:moduleA}
})

  

在模板中使用

现在我们要在模板中使用count状态,要用插值的形式写入

 <h3>{{$store.state.a.count}}</h3>

如果想用简单的方法引入,还是要在我们的计算属性中rutrun我们的状态。写法如下:
computed:{
count(){
return this.$store.state.a.count;
}
},