存图片地址到vuex(笔记)

853 阅读1分钟

开发过程中,有时候会遇到表单中带有上传头像,并有一些选项是需要跳转页面去选择再回来的,所以要求要把开始先上传过的图片地址记录下来,这样跳转页面后,回来数据不会丢失。

先在上传完图片的方法里store.dispatch存一下:

afterRead(file) {
    store.dispatch("user/setImg", this.state.file.file);
}

然后在store的modules文件夹下user.js文件里,先定义一个img:

image.png

getters定义数据:

image.png

mutations定义修改图片的方法:

image.png

actions中定义提交mutations的方法:

image.png

别忘了最后要export default导出去!

使用:

store.getters["user/getImg"]

如果需要清空掉的话:

store.dispatch("user/setImg", "");//清空图片