1.vue的基本认知

1.1.vuex是什么?

vuex是实现全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。

1.2.使用vuex统一管理状态的好处

  1. 能够在vuex中集中管理共享的数据,易于开发和后期维护
  2. 能够高效地实现组件之间的数据共享,提高开发效率
  3. 存储在vuex中的数据都是响应式的,能够实时保持数据与页面的同步

    1.3.什么样的数据适合存储到vuex中

    一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依旧存储在组件自身的data中即可

    1.4.vuex的基本使用

    1.安装vuex依赖包
    1. npm install vuex --save
    2.导入vuex包
    1. import Vuex from 'vuex'
    2. Vue.use(Vuex)
    3.创建store对象
    1. const store=new Vuex.Store({
    2. //state 中存放的就是全局共享的数据
    3. state:{count:0}
    4. })
    4.将store对象挂载到vue实例中
    1. new Vue({
    2. el:'#app',
    3. render:h=>h(app),
    4. router,
    5. // 将创建的共享数据对象,挂载到Vue实例中
    6. // 所有的组件,就可以直接从store中获取全局的数据了
    7. store
    8. })
    5.eslint简单配置
    新建 .prettierrc 文件
    1. {
    2. "semi":false, //去除分号
    3. "singleQuote":true //用单引号替换双引号
    4. }

    2.vuex核心概念

  • State
  • Mutation
  • Action
  • Getter

    2.1 State

    State提供唯一的公共数据源,所有共享的数据都要统一放到 StoreState 中进行存储
    1. // 创建store数据源,提供唯一公共数据
    2. const store=new Vuex.Store({
    3. //state 中存放的就是全局共享的数据
    4. state:{count:0}
    5. })

    1.组件访问 State 中数据的第一种方式:

    1. this.$store.state.全局数据名称

    2.组件访问 State 中数据的第二种方式:

    1. // 1. 从vuex中按需导入 mapState 函数
    2. import { mapState } from 'vuex'
    通过刚才导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的computed计算属性
    1. // 2. 将全局数据,映射为当前组件的计算属性
    2. computed:{
    3. ...mapState(['count'])
    4. }

    2.2 Mutation

    Mutation用于变更 Store 中的数据

①只能通过 mutation 变更 Store 数据,不可以直接操作 Store 中的数据。
②通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化

  1. //定义 Mutation
  2. const store=new Vuex.Store({
  3. state:{
  4. count:0
  5. },
  6. mutations:{
  7. add(state,step){
  8. // 变更状态
  9. state.count+=step
  10. }
  11. }
  12. })

触发 mutations 的第一种方式

  1. // 触发 mutation
  2. methods:{
  3. handle(){
  4. //触发 mutations 的第一种方式
  5. this.$store.commit('add',3) //触发 mutations 时 携带参数
  6. }
  7. }

触发 mutations 的第二种方式

  1. // 1. 从vuex中按需导入 mapMutations 函数
  2. import {mapMutations} from 'vuex'
  1. //2. 将指定的 mutations 函数,映射为当前组件的 methods 函数
  2. methods:{
  3. ...mapMutations(['add','addN'])
  4. }

2.3 Action

Action用于处理异步任务。

如果通过异步操作变更数据,必须通过 Action ,而不能使用 Mutation,但是在 Action中还是要通过触发 Mutation 的方式间接变更数据。

  1. //定义 Action
  2. const store=new Vuex.Store({
  3. //...省略其他代码
  4. mutations:{
  5. add(state,step){
  6. state.count+=step
  7. }
  8. },
  9. actions:{
  10. addAsync(context,step){
  11. setTimeout(()=>{
  12. context.commit('add',step)
  13. },100)
  14. }
  15. }
  16. })

触发 actions 的第一种方式

  1. //触发 Action
  2. methods:{
  3. handle(){
  4. //触发 actions 的第一种方式
  5. this.$store.dispatch('addAsync',5)
  6. }
  7. }

触发 actions 的第二种方式

  1. // 1. 从vuex中按需导入 mapActions 函数
  2. import {mapActions} from 'vuex'
  1. //2. 将指定的 actions 函数,映射为当前组件的 methods 函数
  2. methods:{
  3. ...mapActions(['addAsync','addNAsync'])
  4. }

2.4 Getter

Getter用于对 Store 中的数据进行加工处理形成的数据 不会改变原数据

① Getter 可以对 Store 中已有的数据加工处理之后形成的新的数据,类似vue的计算属性
② Store 中数据发生变化,Getter 的数据也会跟着变化。

  1. //定义 Getter
  2. const store=new Vuex.Store({
  3. state:{
  4. count:0
  5. },
  6. getters:{
  7. showNum:state=>{
  8. return '当前最新的数量是【'+state.count+'】'
  9. }
  10. }
  11. })

使用 getters 的第一种方式

  1. this.$store.getters.名称

使用 getters 的第二种方式

  1. import {mapGetters} from 'vuex'
  2. computed:{
  3. ...mapGetters(['showNum'])
  4. }