0

I have a vuex in module mode that fetching the data of a user:

store/modules/users.js

import axios from "axios";

export const state = () => ({
  user: {}
});

// Sets the values of data in states
export const mutations = {
  SET_USER(state, user) {
    state.user = user;
  }
};

export const actions = {
  fetchUser({ commit }, id) {
    console.log(`Fetching User with ID: ${id}`);
    return axios.get(`${process.env.BASE_URL}/users/${id}`)
      .then(response => {
        commit("SET_USER", response.data.data.result);
      })
      .catch(err => {
        console.log(err);
      });
  }
};

// retrieves the data from the state
export const getters = {
  getUser(state) {
    return state.user;
  }
};

then on my template pages/users/_id/index.vue

<b-form-input v-model="name" type="text"></b-form-input>

export default {
  data() {
    return {
      name: ""
    }
  },
  created() {
    // fetch user from API
    this.$store.dispatch("fetchUser", this.$route.params.id);
  }
}

Now I check the getters I have object getUser and I can see the attribute. How can I assign the name value from vuex getters to the input field?

2 Answers 2

2

watcher is probably what you need

export default {
  // ...
  watch: {
    '$store.getters.getUser'(user) {
      this.name = user.name;
    },
  },
}
Sign up to request clarification or add additional context in comments.

5 Comments

Nice I made it work. Thanks Jacob, in what lifecycle should I call the dispatch? Right now I'm calling it from beforeCreate()
There's no hard rules for this. Normally I do it in created. forum.vuejs.org/t/…
I think that this use case calls for a computed property instead of a watcher
@AllenChun i think this is what Frank means. Not sure if it is what you want though. vuex.vuejs.org/guide/forms.html#two-way-computed-property
@JacobGoh Please check my answer. Regardless of whether you want a two-way binding you should use a computed property over watcher.
0

While Jacob's answer isn't necessarily incorrect, it's better practice to use a computed property instead. You can read about that here

  computed: {
    user(){
        return this.$store.getters.getUser
    }
  }

Then access name via {{user.name}} or create a name computed property

  computed: {
    name(){
        return this.$store.getters.getUser.name
    }
  }

Edit: fiddle as example https://jsfiddle.net/uy47cdnw/

Edit2: Please not that if you want to mutate object via that input field, you should use the link Jacob provided.

2 Comments

in some cases, you would be very right. I just don't think this is the case here. v-model="name" combine with a computed property without setter is going to be problematic. (god... i feel offended by the unreasonable downvote )
Initially my approach is using computed property but I need to mutate the object using input field, then I have 10 fields in my form so I find computed property really tedious. There's also an instance I used computed property to set the state and when I refreshed the page the value suddenly gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.