vuex的学习笔记

什么是Vuex?

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

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

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

npm install vuex --save

要注意的是这里必定要加上 –save,由于你这个包咱们在生产环境中是要使用的。vue

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

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

三、使用咱们vuex,引入以后用Vue.use进行引用。python

Vue.use(Vuex);

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

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

  import store from './vuex/store'数组

五、再而后 , 在实例化 Vue对象时加入 store 对象 :babel

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

Demo

一、如今咱们store.js文件里增长一个常量对象。架构

Vue.use(Vuex)

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

二、用export default 封装代码,让外部能够引用。

export default new Vuex.Store({
  state
})

三、新建一个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>

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

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

  这里的mutations是固定的写法,意思是改变的,因此你先不用着急,只知道咱们要改变state的数值的方法,必须写在mutations里就能够了。

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

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

  

1、 经过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.

2、经过mapState的对象来赋值

  import {mapState} from 'vuex';

  

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

  

3、经过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;
        }
    },
相关文章
相关标签/搜索