vuex 中 actions 如何各个模块间,互相调用 actions、mutations、state 等信息

一、同级 actions 调用

对于同级模块下的 actions,如何互相调用呢?

实现方式:分发 action,action 通过 store.dispatch 方法触发。

二、不同模块 actions 调用

由于 Vuex 使用了单一状态树,应用的所有状态都包含在一个大对象中。那么,随着应用的不断扩展,store 会变得非常臃肿。

为了解决这个问题,Vuex 允许我们把 store 分 module(模块)。每一个模块包含各自的状态、mutation、action 和 getter。

vuex 中 modules 可以将项目的 state 进行分块,互不干扰。那么在单个 moudule 中,actions 如何调用其他模块的 actions 或者根 actions、mutations、state ?

actions 中提供如下方法:

  • rootGetters 用于获取其他模块getter;
  • rootState 用于获取其它模块state;
  • getters 用于获取当前模块getter;
  • state 用于获取当前模块state;
  • dispatch 用于调用action,当前模块和其他模块;
  • commit 用于调用mutation,当前模块和其他模块;

(一)rootGetters

const user = {
  ......
  actions: {
    async changeUser ({ rootGetters }) {
      console.log(rootGetters) // 根 getters 与其他模块 getters 下的信息
    },
  }
}
export default user

(二)rootState

rootState 与上述 rootGetters 同样的,获取根 state 和 其他模块 state 下的信息。

可用于:模块 B 的 actions 里,调用模块 A 的 state。

(三)getters

const user = {
  ......
  getters: {
    name: state => state.username
  },
  actions: {
    async changeUser ({ getters }) {
      console.log(getters) // 当前模块中的 getters 信息
    },
  }
}
export default user

(四)state

state 与上述 getters 同样的,只能获取当前模块中 state 信息。

(五)dispatch

如果同级模块的 action 调用,我们使用 dispatch 来触发;

如果在根文件下,定义一个 log 方法,通过 dipatch 去调用,则可以通过 root 参数:

const user = {
  ......
  getters: {
    name: state => state.username
  },
  actions: {
    async changeTime ({ dispatch }, time) {
      dispatch('log', { time }, { root: true })
    },
  }
}
export default user

根模块 index.js 文件下:

const store = new Vuex.Store({
  actions: {
    log({}, { time }) {
      console.log(time)
    }
  },
})

export default store

模块 B 的 actions,需要调用模块 A 的 actions 时,可以使用 root: true 实现。这里 dispatch 的第一个参数,需要传入其他模块的 actions 路径,第二个参数为传给 actions 的数据,若不传,也需要预留,第三个参数是配置选项,申明这个 actions 不是当前模块的;

(六)commit

commit 触发 mutations,从而同步更新state,如:

const user = {
  state: {
    token: '',
  },
  mutations: {
    setToken: (state, token) => {
      state.token = token
    },
  },
  actions: {
    async Login ({ commit }, userInfo) {
      const { username, password } = userInfo
      const resp = await api.login({ username, password })
      const { tokenInfo } = resp.data
      commit('setToken', tokenInfo.token)
    },
  }
}
export default user

如果想触发其他模块的mutation动态更新state,则需要借助参数root:

首先在根模块 index.js 文件下:

import user from './modules/user'
import home from './modules/home'
const store = new Vuex.Store({
  state: {},
  getters: {},
  mutations: {},
  actions: {},
  /* 最好能够根据功能划分模块,将业务代码写在vuex里,尽可能地将视图层(vue)和领域层(vuex)分离 */
  modules: {
    user,
    home,
  }
})
export default store

然后在 home.js 文件的模块下:

const home = {
  ......
  actions: {
    async changeToken ({ commit }, token) {
      commit('user/setToken', token, { root: true }) // 重点
    },
  }
}
export default home

总结:commit 是调用 mutations 用的,dispatch 是调用 actions 用的,state 是当前模块的 state,而 rootState 是根 state 和其他模块的 state。

  • 9
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在 Vuex ,可以通过在组件使用 `mapActions` 辅助函数来调用模块actions。 首先,在组件引入 `mapActions` 辅助函数: ``` import { mapActions } from 'vuex' ``` 然后,在组件的 `methods` 属性使用 `mapActions` 函数映射 actions: ``` methods: { ...mapActions('moduleName', ['action1', 'action2']) } ``` 其,`moduleName` 是指定模块的名称,`['action1', 'action2']` 是要调用action 的名称数组。 在组件,就可以通过 `this.action1()` 或 `this.action2()` 的方式来调用对应的 action 了。 注意:如果要调用兄弟模块action,需要在根级别的 store 导入并注册该模块,然后就可以在各个模块进行调用了。 ### 回答2: 在Vuex调用兄弟模块actions方法需要通过dispatch方法实现。具体步骤如下: 1. 在需要调用兄弟模块的组件,引入Vuex,并使用mapActions辅助函数将actions方法映射到组件。 2. 在组件的方法,通过dispatch方法调用对应的兄弟模块actions方法。 3. 在兄弟模块定义actions方法,该方法可以接收context对象作为参数,context对象包含了state、commit、dispatch等方法,可以用于访问兄弟模块的状态和调用兄弟模块mutations或actions方法。 4. 在兄弟模块actions方法,可以根据需要执行异步操作,例如发送请求、处理数据等。 总结来说,通过Vuex的dispatch方法,可以在一个模块调用另一个兄弟模块actions方法,并通过context对象进行状态管理和调用其他方法。这样可以实现兄弟模块的数据传递和操作。 ### 回答3: 在VueVuex是一种用于管理应用程序状态的状态管理模式。它包括了一系列的state(状态)、mutations(变化)和actions(动作)。而且,Vuex的适用范围不仅限于在父组件和子组件之共享数据,还可以在兄弟模块实现数据共享。 要调用兄弟模块actions方法,我们需要首先明确哪个兄弟模块actions方法需要被调用。在Vuex,每个模块都有一个名为actions的属性,用于存储该模块actions方法。然后,我们可以使用dispatch方法来调用兄弟模块actions方法。 假设我们有两个兄弟模块A和B,分别有各自的statemutations和actions。如果我们想要在模块A调用模块B的actions方法,可以按照以下步骤进行操作: 1. 首先,在模块A的文件引入Vuex(import Vuex from 'vuex')和模块B(import B from './B')。 2. 在模块A的actions方法通过dispatch方法调用模块B的actions方法。例如: ``` actions: { someAction: ({dispatch}) => { dispatch('B/someActionInB') } } ``` 3. 在模块B的文件定义actions方法someActionInB。例如: ``` actions: { someActionInB: () => { // 执行一些操作 } } ``` 通过以上步骤,我们可以在模块A成功调用模块B的actions方法。这种方式使得各个模块能够更加灵活地进行通信和数据共享,提高了应用程序的可维护性和可扩展性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值