Vue.js 在组件中处理vuex post数据
我有以下vuex突变和动作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)
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);