简体   繁体   中英

Vue js2 vuex update a form v-model values

I have setup vuex and i would like to later fetch the data and update my form model but this fails

In my vuex

  //state
  const state = {
   profile: [],
  }

  //getter
  const getters = {
   profileDetails: state => state.profile,
  }

 //the actions
 const actions = {
    getProfileDetails ({ commit }) {
        axios.get('/my-profile-details')
             .then((response) => {
               let data = response.data;
               commit(types.RECEIVED_USERS, {data});
              },
             );
     }
  }



 const mutations = {
  [types.RECEIVED_USERS] (state, { data }) {
    state.profile = data;
   state.dataloaded = true;
  },

}

Now in my vue js file

export default{

    data: () => ({
       profile_form:{
           nickname:'',
           first_name:'',
           last_name:'',
           email:''
       }

    }),

    computed:{
        ...mapGetters({
            user: 'profileDetails',
        }),

    },

   methods:{
       setUpDetails(){
            this.profile_form.email = this.user.email; //the value is always undefined
        }
    },

    mounted(){
        this.$store.dispatch('getProfileDetails').then(
            (res)=>{
                console.log(res); //this is undefined
             this.setUpDetails(); ///this is never executed
            }
        );
        this.setUpDetails(); //tried adding it here
    }

By checking with the vue developer tools i can see that the vuex has data but my component cant fetch the data in vuex after calling the dispatch in the action to fetch the data.

Where am i going wrong.

Nb: AM using the data to update a form like this

<input  v-model="profile_form.email" >

Your mounted method expects a return (res) from getProfileDetails , but the action isn't returning anything, so you could simply try

 const actions = {
    getProfileDetails ({ commit }) {
      return axios.get('/my-profile-details')
        .then((response) => {
          let data = response.data;
          commit(types.RECEIVED_USERS, {data});
          return data // put value into promise
        },
      );
    }
  }

However, it's more usual to commit to store from within the action (which you are doing) and let the component get the new values from a getter (which you have) - ie one-way-data-flow.

This is how I'd set it up.

data: () => ({
  profile_form:{
    nickname:'',
    first_name:'',
    last_name:'',
    email:''
  }
}),

mounted(){
  this.$store.dispatch('getProfileDetails')
}

computed: {
  ...mapGetters({
    user: 'profileDetails',
  }),
}

watch: {
  user (profileData){
    this.profile_form = Object.assign({}, profileData);
    }
},

methods:{
  submit(){
    this.$store.commit('submituser', this.profile_form)
  }
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM