LoginSignup
1
1

More than 3 years have passed since last update.

Nuxt.jsでstore使う

Last updated at Posted at 2020-12-11

作成

コマンドでstoreファイルのテンプレートを作成する

$ touch store/index.js

ファイルの内容を書き換える

store/index.js
export const state = () => ({
  counter: 0
})

export const mutations = {
  setIncrement (state) {
    state.counter++
  }
}

export const actions = {
  setIncrement (context) {
    context.commit('setIncrement')
  }
}

export const getters = {
  getCounter(state) {
    return state.counter
  }
}

使用

// stateの取得
this.$store.state.count

// mutationsの実行
this.$store.commit('setIncrement')

// actionsの実行
this.$store.dispatch('setIncrement')

// gettersの実行
this.$store.getters.getCounter

Nuxt.jsのそのほか

1
1
0

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
1