Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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中查找/替换特定对象?_Vue.js_Vuejs2_Vuex - Fatal编程技术网

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个预加载程序广告,我需要用真实图像替换预加载程序。