1
1

More than 1 year has passed since last update.

vue.jsのvuexでthis.$store.dispatchに関するメモ

Posted at

Vuexは、Vue.jsアプリケーションで状態の管理を行うためのライブラリです。その中で、dispatchメソッドは、Vuexのストア内で定義されたアクションを実行するために使用されます。

アクションは、ストアの状態を変更するための関数です。アクションを使用することで、ストアの状態を変更することができます。

以下は、Vuexでアクションを使用する例です。

// ストア内でアクションを定義する
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})

// コンポーネント内でアクションを実行する
methods: {
  increment () {
    this.$store.dispatch('increment')
  }
}

上記の例では、incrementアクションを実行することで、ストア内のcount状態をインクリメントすることができます。

一般的に、アクションは、非同期処理を行う場合や、複数のミューテーションを実行する場合などに使用されます。ただし、単純な状態の変更の場合は、ミューテーションを使用することが推奨されます。

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