0
1

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 1 year has passed since last update.

vuexでuser情報をグローバルに使用するためのメモ

Posted at

vueでコンポーネント間のデータ使用で、propsやemitを使いますが、
vuexを使って、どこでもデータを呼び出すことができます。
user情報の保持はよく使うので、忘れないようにメモ。

user情報を保存するための関数

src/store/index.js

 mutations: {
    user: (state, data) => (state.user = data),
  },
  actions: {
    setUserData: ({ commit }, data) => {
      commit('user', data)
    },
  },

ユーザー情報を保存

// ユーザー情報をVuexに保存する
  store.dispatch('setUserData', response.data)

ユーザー情報を呼び出す

this.user = this.$store.state.user
0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?