Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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
Vue.js 在组件中处理vuex post数据_Vue.js_Vuex - Fatal编程技术网

Vue.js 在组件中处理vuex post数据

Vue.js 在组件中处理vuex post数据,vue.js,vuex,Vue.js,Vuex,我有以下vuex突变和动作 mutations: { //CRUD Mutations CRUD_FETCH_PROPERTY: (state, response) => { state.properties = response }, CRUD_CREATE_PROPERTY: (state, response) => { state.properties.push(response)

我有以下vuex突变和动作

mutations: {
    //CRUD Mutations
    CRUD_FETCH_PROPERTY: (state, response) => {        
      state.properties = response
    },
    CRUD_CREATE_PROPERTY: (state, response) => {        
      state.properties.push(response)
    },
    CRUD_UPDATE_PROPERTY: (state, response) => {        
     state.properties.push(response)
    },
    CRUD_DELETE_PROPERTY: (state, response) => {        
      state.properties.push(response)
    },
    CRUD_FILTER_PROPERTY: (state, response) => {        
      state.properties.push(response)
    }

  },
  actions: {
    //CRUD Actions
    async CrudFetchProperty({ commit }) {
    const response = await axios.get(
      'https://example.com/users/json_endpoint'
    );
    //Fetch and add properties to store
    commit('CRUD_FETCH_PROPERTY', response.data);
    },
    
    async CrudCreateProperty({ commit }) {
    const response = await axios.post(
      'https://example.com/users/create_property'
    );
    //Post and add property to store
    commit('CRUD_CREATE_PROPERTY', response.data);
    }
  },
CRUD\u FETCH\u属性
按预期工作,我可以从api获取记录。创建属性时,我是从
home.vue

在home.vue文件的“脚本”部分中,我有以下内容

 methods: {                                   
    addProperty: function() {
                var optionAxios = {
                headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
                }
                };
                axios.post('https://example.com/users/create_property', {
                        country: this.country,
                        name: this.name,
                        stars: this.stars,
                        city: this.city,
                        region: this.region,
                        ptype: this.ptype,
                        pdatecreated: this.pdatecreated,
                        pdateupdated: this.pdateupdated,
                        pcreator: this.pcreator
                },optionAxios)
                .then(function (response) {
                    //console.log(response);
                    //this.$store.dispatch('CrudCreateProperty')
                    //this.$store.commit('CRUD_CREATE_PROPERTY', response.data);
                })
                .catch(function (error) {
                   console.log(error);
                });

            this.country = '',
            this.name = '',
            this.stars = '',
            this.city = '',
            this.region = '',
            this.ptype = '',
            this.pdatecreated = '',
            this.pdateupdated = '',
            this.pcreator = ''
    }
  }
添加时,是否必须调用创建属性操作并将post数据作为参数传递

this.$store.dispatch('CrudCreateProperty', postData)
或者我可以在这个级别调用commit,这是一个好的实践吗

this.$store.commit('CRUD_CREATE_PROPERTY', response);