详解Vuex

本文目录:

  • 1.Vuex的定义
  • 2.Vuex的使用流程
  • 3.Vuex的核心概念
  • 4.辅助函数mapState、mapGetters、mapMutations、mapActions
  • 5.Module
  • 6.实现Vuex的四个模块拆分

1.Vuex的定义

Vuex是什么?官方定义:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。
Vue中跨组件的数据交互可以使用LocalStorage缓或者SessionStorage缓存,但是,这不是推荐的做法,也不方便,容易丢失数据或者是数据紊乱(因为没有及时清理与回收)

2.Vuex的使用流程

1 安装Vuex

npm install vuex --save

2 在src目录下,新建store文件夹,在store文件夹下新建index.js文件
3 在index.js文件中输入以下代码

import Vue from 'vue'
import Vuex from 'vuex'
// 让vuex作为vue的插件来使用
Vue.use(Vuex)
// 创建一个容器
let store = new Vuex.Store()
// 把这个store实例导出 方便在vue中注入
export default store

4 把store实例注入到vue中,在main.js中引入store

import Vue from 'vue'
import App from './App'
import router from './router'
import '@/assets/style/index.css'
//************************************************
import store from '@/store/index'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
    //************************************************
  store,
  render: h => h(App)
})

接下来我们需要去使用vuex管理状态了,步骤
1、设置一个共享数据,在store/index.js里进行操作

// 创建一个容器
let store = new Vuex.Store({
  state: {
    goods_num: 0
  }
})

2、在对应的组件中通过this.$store.state.goods_num就可以使用这个数据
3、组件中如果要修改.goods_num,则需要提交一个mutation,这个mutation类似于js中的事件,有事件类型和事件处理函数,我们在事件处理函数中去更改全局的goods_num
比如我们在组件通过通过定义有一个addNum事件去修改goods_num
以下是组件中的代码

<script type="text/ecmascript-6">
export default {
  data () {
    return {
      num: 12
    }
  },
  methods: {
    increase () {
      this.num++
    },
    decrease () {
      this.num--
    },
    addNum () {
        //唯一去修改state数据的方法,就是提交一个mutation
        //this.$store.state.goods_num+=2000
        //上面这行代码也可以进行,但是不推荐,因为如果在store中加入严格模式的话,就无法生效了(strict:true)
        //如果是想同时传多个参数,可以传一个对象
        //commit()中也可以整个传一个对象,{type:'changeCar',...}
      this.$store.commit('changeNum', this.num)
    }
  }
}
</script>

4、在store里面去增加一个mutation, 在store/index.js里面操作

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

// 让vuex作为vue的插件来使用
Vue.use(Vuex)
// 创建一个容器
let store = new Vuex.Store({
  state: {
    goods_num: 0
  },
  mutations: {
      //state就是上面的数据对象,num就是负责接收参数的形参
    changeNum (state, num) {
      state.goods_num += num
    }
  }
})

// 把这个store实例导出 方便在vue中注入
export default store

3.Vuex的核心概念

store: 每一个 Vuex 应用的核心就是 store(仓库)。“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state)

state:包含所有应用级别状态的对象

Getter: 在组件内部获取store中状态的函数,可以认为是 store 的计算属性

Mutation: 唯一修改状态的事件回调函数

Action:Action 类似于 mutation,不同在于:1、Action 提交的是 mutation,而不是直接变更状态。2、Action 可以包含任意异步操作

Modules: 将store分割成不同的模块

Mutation

注意; 在Mutation中,是不能提交异步代码的,例如:

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

// 让vuex作为vue的插件来使用
Vue.use(Vuex)

// 创建一个容器
let store = new Vuex.Store({
  state: {
    goods_num: 0
  },
  mutations: {
    changeNum (state, num) {
      console.log(state)
      setTimeout(() => {
        state.goods_num += num
      }, 2000)
    }
  }
})

// 把这个store实例导出 方便在vue中注入
export default store

Action

在mutation中提交异步代码,状态的改变是没办法追踪的,如果有异步代码,需要放到Action中去,等异步代码执行完成后再提交 store/index.js中的代码

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

// 让vuex作为vue的插件来使用
Vue.use(Vuex)

// 创建一个容器
let store = new Vuex.Store({
  state: {
    goods_num: 0
  },
  mutations: {
    changeNum (state, num) {
      state.goods_num += num
    }
  },
  actions: {
    changeNumAction (context, num) {
      console.log(context)  
      setTimeout(() => {
          //用comtext去调用commit,再去触发mutations
        context.commit('changeNum', num)
      }, 2000)
    }
  }
})

// 把这个store实例导出 方便在vue中注入
export default store

在对应的组件中通过dispatch去触发这个actions

export default {
  data () {
    return {
      num: 12
    }
  },
  components: {

  },
  methods: {
    increase () {
      this.num++
    },
    decrease () {
      this.num--
    },
    addNum () {
      // this.$store.commit('changeNum', this.num)
      this.$store.dispatch('changeNumAction', this.num)
    }
  }
}

Getter

Getter可以认为是 store 的计算属性,可以对数据进行处理。为了掩饰效果,我们新建一个Count组件来说明
组件中的computed是局部的
getter可以认为是一个全局的计算属性

<template>
  <div class="page">
    <span>{{this.$store.state.count}}</span>
    <button @click="increase">+</button>
  </div>
</template>
<script type="text/ecmascript-6">
export default {
  data () {
    return {
    }
  },
  components: {
  },
  methods: {
    increase () {
      this.$store.commit('add')
    }
  }
}
</script>
<style scoped>
.page{
  width: 300px;
  margin: 100px auto
}
</style>

在store中增加mutation函数和count变量,以下是部分代码

  state: {
    goods_num: 0,
    count: 0
  },
  mutations: {
    changeNum (state, num) {
      state.goods_num += num
    },
    add (state) {
      state.count++
    }
  }

这样我们就实现了vuex版的加法计数器,如果对count这个变量有一些逻辑判断怎么办呢?我们可以在组件内部使用computed来实现,如果这个逻辑是全局通用的,你可以把它放到Getter里面去,举例:
需求:当计数器加到10的时候就不能继续加了
在store中增加getters选项

getters: {
    newCount (state) {
      return state.count > 10 ? 10 : state.count
   }
 }
})

使用到时候,从store.getters对象中去获取

<p><span>{{this.$store.getters.newCount}}</span></p>

注意:如果要给getters里面的函数传参数,需要写成这样

  getters: {
    newCount (state) {
      return state.count > 10 ? 10 : state.count
    },
    newCount2 (state) {
      // 返回一个函数
      return (num) => state.count > num ? num : state.count
    }
  }

调用的时候,这样调用

<p><span>{{this.$store.getters.newCount2(5)}}</span></p>

4.辅助函数mapState、mapGetters、mapMutations、mapActions

这几个辅助函数可以帮助我们简化一些写法

注意:在使用之前一定要先引入(在要使用的子组件中引入)

import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'

1.mapState,这个函数是computed的映射,可以把传入mapState的对象或者数组,转化成计算属性

mapState里面可以传入数组和对象,首先来看传入数组的写法

computed: {
  ...mapState(['num3', 'num4', 'num5'])
}
//这样的话,在子组件中就可以直接使用了

注意,这里传入的num3、num4、num5需要和state里面定义的状态同名

state: {
  goods_num: 0,
  count: 0,
  num3: 9,
  num4: 10,
  num5: 11
}

这样写就可以直接去批量拿到state里面的数据,不用再去使用this.$store.state.num3 来获取值了,这就是辅助函数的作用,可以简化一些写法
有些时候,我们需要从state里面取值进行处理,例如这样:

computed: {
  num3 () {
    return this.$store.state.num3 > 10 ? 10 : 20
  }
}

如果使用辅助函数,我们就需要以对象的形式传入了

computed: {
  // num3 () {
  //   return this.$store.state.num3 > 10 ? 10 : 20
  // }
  ...mapState({
    num3: state => state.num3 > 10 ? 10 : 20
  })
}

使用箭头函数可以简化代码,同时还可以给state里的状态重命名,例如:

computed: {
  // num3 () {
  //   return this.$store.state.num3 > 10 ? 10 : 20
  // }
  ...mapState({
    num3: state => state.num3 > 10 ? 10 : 20,
    num100: 'num4'   //给num4 取一个别名 num100
  })
}

如果你要使用this这个关键字,就不能用箭头函数,因此,可以简写成这样

computed: {
  // num3 () {
  //   return this.$store.state.num3 > 10 ? 10 : 20
  // }
  ...mapState({
    num3: state => state.num3 > 10 ? 10 : 20,
    num100: 'num4',
    num6 (state) {
      //需要取data里面的num99和state里面的num6相加,这个时候需要用到this
      return this.num99 + state.num6
    }
  })
}

以上就是mapState的基本用法,mapMutations、mapGetters、mapActions的用法也一样, 简单举例:

//mapMutations的用法
methods: {
  // increase () {
  //   this.$store.commit('add')
  // }
  ...mapMutations({
    increase: 'add'
  }),
  // decrease () {
  //   this.$store.dispatch('decreaseAction')
  // },
//mapActions的用法
  ...mapActions({
    decrease: 'decreaseAction'
  })
},
    
//mapState的用法
computed: {
    // num3 () {
    //   return this.$store.state.num3 > 10 ? 10 : 20
    // }
    ...mapState({
      num3: state => state.num3 > 10 ? 10 : 20,
      num100: 'num4',
      num6 (state) {
        return this.num99 + state.num6
      }
    }),
//mapGetters的用法
    ...mapGetters({ 
      num5: 'newCount'
    })
  }

5.Module

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

为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块, 下面是我们的store/index.js文件,我们可以尝试将其拆分一个子模块出来

主要步骤

  • 自定义一个对象(子模块)
  • 将这个对象挂在store上

完整代码:

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

// 让vuex作为vue的插件来使用
Vue.use(Vuex)

// 1 自定义一个对象(子模块)
let countModule = {
  state: {
    count: 0,
    num3: 9,
    num4: 10,
    num5: 11,
    num6: 6
  },
  mutations: {
    add (state) {
      state.count++
    },
    decreaseMutation (state) {
      state.count--
    }
  },
  actions: {
    decreaseAction (context) {
      setTimeout(() => {
        context.commit('decreaseMutation')
      }, 1000)
    }
  },
  getters: {
    newCount (state, getters, rootState) {
      return state.count > 10 ? 10 : state.count
    },
    newCount2 (state) {
      return (num) => state.count > num ? num : state.count
    }
  }
}

// 创建一个容器
let store = new Vuex.Store({
  state: {
    goods_num: 0
  },
  mutations: {
    changeCar (state, num) {
      state.goods_num += num
    }
  },
  actions: {
    changeCarAction (context, num) {
      console.log(context)
      setTimeout(() => {
        context.commit('changeCar', num)
      }, 2000)
    }
  },
//2 将这个对象挂在store上
  modules: {
    countModule
  }
})

// 把这个store实例导出 方便在vue中注入
export default store

//子组件使用的时候怎么用?
//{{this.$store.state.countModule. goods_num}}
//就是需要多加一个模块的名字

6.实现Vuex的四个模块拆分

创建四个分模块

const actions={}
export default  actions

const getters = {}
export default getters

const mutations = {}
export default mutations

const state = {}
export default state

index.js中的代码

import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import state from './state'
import mutations from './mutations'
import getters from './getters'
Vue.use(Vuex)
const store = new Vuex.Store({
    state,
    actions,
    mutations,
    getters
})
export default store

上面这些都是创建vuex的基本代码
接下来在main.js中把store注册进去

import store from './store'

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

至此,vuex的模块拆分完成,接下来就可以正常使用vuex进行开发了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容

  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 730评论 0 3
  • Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有...
    skycolor阅读 837评论 0 1
  • Vuex是什么? Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件...
    萧玄辞阅读 3,117评论 0 6
  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 2,937评论 0 7
  • 一番折腾 我就这样在凌晨的夜里醒着 空气在叶子里打着旋 来到我的皮肤 他的脸就在我面前 我想听听他的呼吸 我想看看...
    假的稻草人阅读 159评论 0 2