美文网首页
从头开始学Vuex

从头开始学Vuex

作者: A郑家庆 | 来源:发表于2021-07-14 20:12 被阅读0次
state、getters、mutations、actions对比图

默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的,所以这几个都默认是全局状态,如果所有文件都有namespaced: true,那么获取全局文件中状态或方法都跟namespace: false一致,除了在actions中调用子模块中的方法时还是需要用到{root: true}注意:mapState和mapGetters是放在computed中,mapMutations、mapActions是放在methods中

Module

命名空间

默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的——这样使得不同模块相同命名能够对同一 mutation 或 action 作出响应.
如果希望你的模块具有更高的封装度和复用性,你可以通过添加namespaced: true的方式使其成为带命名空间的模块.当模块被注册后,它的所有getter、action、mutation都会自动根据模块注册的路径调整命名.

在带命名空间的模块内访问全局内容

若需要在全局命名空间内分发 action 或提交 mutation,将 { root: true } 作为第三参数传给 dispatch 或 commit 即可.

    actions: {
      // 在这个模块中, dispatch 和 commit 也被局部化了
      // 他们可以接受 `root` 属性以访问根 dispatch 或 commit
      someAction ({ dispatch, commit, getters, rootGetters }) {
        getters.someGetter // -> 'foo/someGetter'
        rootGetters.someGetter // -> 'someGetter'
        dispatch('someOtherAction') // -> 'foo/someOtherAction'
        dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'
        commit('someMutation') // -> 'foo/someMutation'
        commit('someMutation', null, { root: true }) // -> 'someMutation'
      },
      someOtherAction (ctx, payload) { ... }
    }
  }
在带命名空间的模块注册全局 action

若需要在带命名空间的模块注册全局 action,你可添加 root: true,并将这个 action 的定义放在函数 handler 中。例如:

  modules: {
    foo: {
      namespaced: true,
      actions: {
        someAction: {
          root: true,
          handler (namespacedContext, payload) { ... } // -> 'someAction'
        }
      }
    }
  }
带命名空间的绑定函数

当使用 mapState, mapGetters, mapActions 和 mapMutations 这些函数来绑定带命名空间的模块时,写起来可能比较繁琐:

computed: {
  ...mapState({
    a: state => state.some.nested.module.a,
    b: state => state.some.nested.module.b
  })
},

对于这种情况,你可以将模块的空间名称字符串作为第一个参数传递给上述函数,这样所有绑定都会自动将该模块作为上下文。于是上面的例子可以简化为:

computed: {
  ...mapState('some/nested/module', {
    a: state => state.a,
    b: state => state.b
  })
},

而且,你可以通过使用 createNamespacedHelpers 创建基于某个命名空间辅助函数。它返回一个对象,对象里有新的绑定在给定命名空间值上的组件绑定辅助函数:

import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('some/nested/module')
export default {
  computed: {
    // 在 `some/nested/module` 中查找
    ...mapState({
      a: state => state.a,
      b: state => state.b
    })
  }
}

subscribe

订阅 store 的 mutation。handler 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:

store.subscribe((mutation, state) => {
  console.log(mutation.type)
  console.log(mutation.payload)
})

subscribeAction

订阅 store 的 action。handler 会在每个 action 分发的时候调用并接收 action 描述和当前的 store 的 state 这两个参数:

store.subscribeAction((action, state) => {
  console.log(action.type)
  console.log(action.payload)
})

从 3.1.0 起,subscribeAction 也可以指定订阅处理函数的被调用时机应该在一个 action 分发之前还是之后 (默认行为是之前):

store.subscribeAction({
  before: (action, state) => {
    console.log(`before action ${action.type}`)
  },
  after: (action, state) => {
    console.log(`after action ${action.type}`)
  }
})

双向绑定的计算属性

<input v-model="message">
computed: {
  message: {
    get () {
      return this.$store.state.obj.message
    },
    set (value) {
      this.$store.commit('updateMessage', value)
    }
  }
}

相关文章

  • 从头开始学Vuex

    默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的,所以这几个都默认...

  • 跟着文档学Vuex(三):状态过滤Getters

    上一期跟着文档学Vuex(二):状态读取我们讲到如何将数据储存到vuex并通过computed读取,但是有时候我们...

  • 从头开始学绘画

    日更写作30天的小目标完成了,对自己的毅力更有信心,所以开始拟定下一个目标。准备文房四宝,从头开始学画画。 刚开始...

  • 从头开始学英语

    虽然大学时考过了大学英语六级,但英语的听说能力一直很弱,毕业后,工作用不上,日常的电影等也基本有中文字幕,所以一直...

  • 从头开始学Git

    常用命令 git init 初始化git仓库git add 添加文件git status 查看...

  • 从头开始学Vue

    模版语法 不管是使用v-指令绑定attribute还是Mustache语法,后面可以跟值或者js表达式,如果是事件...

  • 从头开始学Webpack

    npm下代码-D和-S的区别 -S 就是 --save 的简写,会将包的名称及版本号放在dependencies里...

  • 从头开始学写作

    不知不觉在日更的路上这么久,还以为坚持不下来,可目前来看,算的上差强人意,原来一旦形成习惯,事情就简单多了,困难还...

  • VUEX基本介绍,包含实战示例及代码(基于Vue2.X)

    VUEX 1 VUEX基本介绍 1.1 官方API 1.2 什么是vuex 1.3 Vuex使用场景 1、Vuex...

  • 【文档笔记】-Vuex

    什么是vuex? vuex文档: https://vuex.vuejs.org/zh/[https://vuex....

网友评论

      本文标题:从头开始学Vuex

      本文链接:https://www.haomeiwen.com/subject/xzlspltx.html