0
0

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.

Vuex メモ書き

0
Posted at

Vuex

  • 兄弟や親子のコンポーネントに値を共有する

  • Vueのコンポーネント内ではコンポーネントの画面で使う情報だけを扱い、APIからデータを取得する部分は全てVuexに委譲するのが良いよ

  • コンポーネント横断で状態を保持してしまうと、インプットとアウトプットがごちゃごちゃになり破綻してしまう

  • props は深く入れ子になったコンポーネントには渡すの面倒で、兄弟には渡せない

  • 親子のインスタンスを直接参照したり、イベントを介して複数の状態のコピーを変更、同期することはやってはいけないよ

  • ストアパターン

    • 小規模なアプリであればこれでOK
// 単純ストアパターン
var store = {
  debug: true,
  state: {
    message: 'Hello!'
  },
  setMessageAction (newValue) {
    if (this.debug) console.log('setMessageAction triggered with', newValue)
    this.state.message = newValue
  },
  clearMessageAction () {
    if (this.debug) console.log('clearMessageAction triggered')
    this.state.message = ''
  }
}
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?