LoginSignup
27
27

More than 5 years have passed since last update.

Nuxt.js vuex モジュールモードでのgettersの使用方法

Last updated at Posted at 2018-08-26

忘れそうなので覚書を。

gettersとは

要は、vuexのstateを取得する際のゲッターメソッドです。

クラシックモードでの使用方法

まずはstoreの定義です。

store/hoge.js
const store = () => new Vuex.Store({

  state: {
    hoge: null
  },
  getters: {
    isHogeExist (state) {
      return !!state.hoge
    }
  }
)

次に使用する側です。(抜粋)

pages/index.vue
<v-btn to="/hogehoge" v-show="$store.getters.isHogeExist">ほげ</v-btn>

モジュールモードでの使用方法

同じようにstoreの定義

store/hoge.js
export const state = () => ({
  hoge: null
}

export const getters = {
  isHogeExist (state) {
    return !!state.hoge
  }
}

使用するとき

pages/index.vue
<v-btn to="/hogehoge" v-show="$store.getters['hoge/isHogeExist']">ほげ</v-btn>

mutationと同じようにハッシュキーを指定する形で呼びだすようですね。:sunny:

参考リンク

27
27
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
27
27