LoginSignup
1
0

More than 5 years have passed since last update.

Vuexのactionをactionの中で再帰的に呼ぶ

Posted at

Vuexのactionをデータがロードし終わるまで再帰的に呼び出す

Vuexのアクションのコンテキストオブジェクトについて

https://vuex.vuejs.org/ja/guide/actions.html
actioonの第一引数に渡されるコンテキストオブジェクトは
dispatchやstateを呼ぶことが可能

実装

stateのitemsを見てlengthが0ならもう一度loopを呼ぶ
別の場所でstate.itemsに値が入ったらreturn

export default {
  state {
    items: []
  },
  actions {
    loop({ stage, dispatch }) {
      if (state.items.length > 0) {
        console.log('loaded');
        return;
      } else {
        setTimout(() => dispatch.loop(), 500);
      }
    }
  }
}
1
0
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
0