程序员

浅谈vuex

2019-03-25  本文已影响0人  天天zzl

什么是vuex?

  1. Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应
    用的所有组件的状态,以利用 Vue.js 的细粒度数据响应机制来进行高效的状态更
    新。并以相应的规则保证状态以一种可预测的方式发生变化。

  2. 借鉴 了Flux、Redux、 The Elm Architecture

  3. 一个 Vuex 应用的核心是 store(仓库,一个容器),store包含着你的应用中大部分的状态 (state)。

4.Vuex 也集成到 Vue 的官方调试工具https://github.com/vuejs/vue-devtools

2019-03-25_161324.png

什么情况下应该使用 Vuex?

  1. 不适用:小型简单应用,用 Vuex 是繁琐冗余的,更适合使用简单的组件通信。
  2. 适用于:如果状态较多的项目都会用到Vuex,更加的方便,如果组件嵌套层级过深、多个组件依赖了同一个状态,那么必然要使用Vuex更加的方便
其实在实际开发的项目中, 都是Vuex和组件通信混合的

安装:

npm install vue
npm install vuex

使用:

1.引入vue              import Vue from "vue"
2.引入vuex             import Vuex from "vuex"
3.                     Vue.use(Vuex)
4.定义一个容器          const store=new Vuex.Store({ // (注意):在应用中是唯一的,只能定义一个
                  
                       })  
5.记住一定得导出        export default store
6.注意还得把自定义的store注入到Vue的根实例中也就是main.js中      new Vue({
                                                                  el: '#app',
                                                                  router,
                                                                  store,
                                                                  components: { App },
                                                                  template: '<App/>'
                                                               })
                                                 

核心概念:

Store

State

//在state中定义状态
const store = new Vuex.Store({
  state: {
    shopList:[
           {
              id:1,
              count:1,
              price:¥12,
              name:'鱼香肉丝'
           },
           {
              id:2,
              count:3,
              price:¥10,
              name:'大盘鸡'
           },
           {
              id:3,
              count:3,
              price:¥20,
              name:'煎饼果子'
           },
       ]    // 这里的 shopList 就是状态,之前称之为数据  (数据 === 状态)
  }
})

//( 要使用的时候 )  在要使用定义的状态的组件中
//通过计算属性(computed)去获取仓库中定义的数据
computed: {
    getShopList () {
      return this.$store.state.shopList
    }
  }

Getter

const store = new Vuex.Store({
//State
  state: {
    shopList:[
           {
              id:1,
              count:1,
              price:¥12,
              name:'鱼香肉丝'
           },
           {
              id:2,
              count:3,
              price:¥10,
              name:'大盘鸡'
           },
           {
              id:3,
              count:3,
              price:¥20,
              name:'煎饼果子'
           },
       ]    // 这里的 shopList 就是状态,之前称之为数据  (数据 === 状态)
  }
})
//Getters
 getters:{
    //总价
    totalPrice (state) {
      let price=0
      state.ShopList.map(item=>{
        price+=item.price*item.count
      })
      return price
    }
}
})


//通过计算属性(computed)去获取仓库中定义的数据
computed: {
    getShopList () {
      return this.$store.state.shopList
    },
   getTotalPrice(){
      return this.$store.getters
   }
  }

Mutation

//在组件中
this.$store.commit('addIncrement', 5)
const store = new Vuex.Store({
//State
  state: {
    shopList:[
        //同上
       ]    // 这里的 shopList 就是状态,之前称之为数据  (数据 === 状态)
  }
})
//Mutations
  mutations:{
       addIncrement(state,5){
      
            //操作逻辑    

         }
  }
//Getters
   getters:{
       //同上
   }
})

Action

Action 类似于 mutation,不同在于:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation,或者通过 context.statecontext.getters 来获取 state 和 getters。当我们在之后介绍到 Modules 时,你就知道 context 对象为什么不是 store 实例本身了。

Module

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。

为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:

通过computed取值的时候需要加上子模块的名称,除此之外都无需添加子模块名称

详解请到Modules

上一篇 下一篇

猜你喜欢

热点阅读