Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vue js2 vuex更新表格v-model值_Javascript_Vuejs2_Vuex - Fatal编程技术网

Javascript Vue js2 vuex更新表格v-model值

Javascript Vue js2 vuex更新表格v-model值,javascript,vuejs2,vuex,Javascript,Vuejs2,Vuex,我已经安装了vuex,我想稍后获取数据并更新表单模型,但失败了 在我的vuex中 //state const state = { profile: [], } //getter const getters = { profileDetails: state => state.profile, } //the actions const actions = { getProfileDetails ({ commit }) {

我已经安装了vuex,我想稍后获取数据并更新表单模型,但失败了

在我的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;
  },

}
现在在我的vue js文件中

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
    }
通过使用vue developer工具进行检查,我可以看到vuex中有数据,但我的组件在操作中调用dispatch以获取数据后无法在vuex中获取数据

我哪里做错了

注意:我正在使用这些数据更新这样的表单

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

您装载的方法需要从
getProfileDetails
返回(res),但操作没有返回任何内容,因此您可以简单地尝试

 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
        },
      );
    }
  }
但是,更常见的做法是从操作(您正在执行的操作)中提交存储,并让组件从getter(您拥有的)中获取新值,即单向数据流

我就是这样安排的

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)
  }
},