sunmingtao / sample-code

3 stars 4 forks source link

Vue js -- state.user is undefined #112

Closed sunmingtao closed 4 years ago

sunmingtao commented 4 years ago

Follow https://www.udemy.com/course/vuejs-2-the-complete-guide/learn/lecture/8349448#overview --- Sending the token to the backend.

Email never displays even after user is authenticated

sunmingtao commented 4 years ago

Turns out I forgot to add user as an attribute of state Incorrect

state: {
    idToken: null,
    userId: null,
  },

Correct

state: {
    idToken: null,
    userId: null,
    user: null,
  },

Even though the following code adds user as an attribute to state

storeUser (state, user) {
      state.user = user
    }

without user defined in state initially, the getters.user is not invoked.

getters: {
    user (state) {
      console.log("getters state" +state.user);
      return state.user;
    }
  }
sunmingtao commented 4 years ago

This is due to the fact that Vue cannot detect newly added attributes.

Manually added attributes are not reactive.

https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats