27
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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:

##参考リンク
https://github.com/nuxt/nuxt.js/issues/848

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?