32.Module2

在有namespace的module内访问全域内容(Global Assets):
如果你希望使用全局 state 和 getter,rootState 和 rootGetters 会作为第三和第四参数传入 getter,也会通过 context 对象的属性传入 action。

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

modules: {
  foo: {
    namespaced: true,

    getters: {
      // 在这个模块的 getter 中,`getters` 被局部化了
      // 你可以使用 getter 的第四个参数来调用 `rootGetters`
      someGetter (state, getters, rootState, rootGetters) {
        getters.someOtherGetter // -> 'foo/someOtherGetter'
        rootGetters.someOtherGetter // -> 'someOtherGetter'
      },
      someOtherGetter: state => { ... }
    },

    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) { ... }
    }
  }
}

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

{
  actions: {
    someOtherAction ({dispatch}) {
      dispatch('someAction')
    }
  },
  modules: {
    foo: {
      namespaced: true,

      actions: {
        someAction: {
          root: true,
          handler (namespacedContext, payload) { ... } // -> 'someAction'
        }
      }
    }
  }
}

<<:  Day32 | 在WebView里使用Router的问题与解法!

>>:  Day33. 迭代器模式

[Day 1] 全民疯AI系列2.0-机器学习实战手册

全民疯AI系列2.0 第13届iT邦帮忙铁人赛 前言 哈罗大家好我是10程序中的10!我是上一届铁人...

Day28 :【TypeScript 学起来】React + TypeScript 实作简单 Todo App Part1

前面笔记了那麽多,终於来实作看看了~先来做个简单的 to do app,也会纪录实作上遇到的问题。...

Day15 Android - fragment介绍

fragment,中文有分段、碎片的意思,可透过activity中,加入、切换多个不同的fragme...

大脑如何精准学习 (2) 积极主动参与/好奇心

终於读到我很想读的段落了。 积极主动参与 active engagement - 定义 这里讲的是「...

003-转职

决定转职到 UX/UI 设计的这个过程,花费了不少的时间。 知道自己的作品中,纯设计相关的工作,占了...