LoginSignup
1
0

More than 1 year has passed since last update.

Vuexで別のモジュールを呼び出す

Last updated at Posted at 2022-03-17

storeのactionから他のモジュールを呼び出したい

storeの更新をする際、別のモジュールも一緒に更新しようとしたのですが、
うまく出来ず時間を食ったので備忘録。

データを取得するとき

vue.js
export const actions = {
    getFunction ({ state, rootGetters }, payload) {
        // このモジュール内のデータを取得
        const localData = state.dataList

        // 別のモジュールからデータを取得
        const adminData = rootGetters["modules/admin/dataList"]
    }
}

他のモジュールにdipatchするとき

vue.js
export const actions = {
    saveFunction ({ dispatch }, payload) {
        dispatch('modules/admin/dataList', payload, { root: true })
    }
}

このとき、dispatchの第3引数に{ route: true }をつけてあげれば良い。
ないとダメで、これでハマりました。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0