Vue.js 如何在Vuex中查找/替换特定对象?
我通过Vue.js 如何在Vuex中查找/替换特定对象?,vue.js,vuejs2,vuex,Vue.js,Vuejs2,Vuex,我通过action和mutation上传了简单的图像。主要的是,我首先要存储预加载程序,然后,当图像上传到服务器时,显示图像并删除预加载程序 export default new Vuex.Store({ state: { images: [], }, mutations: { addImage(state, image) { state.images.push(image); } },
action
和mutation
上传了简单的图像。主要的是,我首先要存储预加载程序,然后,当图像上传到服务器时,显示图像并删除预加载程序
export default new Vuex.Store({
state: {
images: [],
},
mutations: {
addImage(state, image) {
state.images.push(image);
}
},
actions: {
saveImage({commit}, image) {
commit('addImage', {url: 'http://via.placeholder.com/100x100?text=loading...'});
let form_data = new FormData();
form_data.append('file', image);
return new Promise((resolve, reject) => {
jQuery.ajax({
url : '/api/v1/products/files/save',
data : form_data,
type : 'POST',
cache: false,
contentType: false,
processData: false,
success: response => {
// commit('addImage', response.file);
// here I somehow need to replace/readd the response.file
resolve(response.file)
}
}).fail(() => reject())
})
}
},
strict: debug
})
我基本上可以使用
数组。查找方法来查找url:http://via.placeholder.com/100x100?text=loading...
删除它,然后添加新的,但不确定它是否是正确的方法 你给vuex的动作太多了。让它完成一项任务——上传文件。即使没有vuex状态,占位符/预加载程序也应由组件处理。组件需要知道的只是图像是否正在上载,以便显示加载指示器。由于您的操作返回一个承诺,您的组件将知道何时准备就绪。每个图像都显示预加载程序。例如,如果我首先选择10个图像,每次上传图像时,我都会显示10个预加载程序广告,我需要用真实图像替换预加载程序。