1.新建js文件
const xxx= { namespaced: true, state: { formData: { //此处储存数据 } }, getters: { }, mutations: { CHANGE_FORMDATA: (state, value) => { state.formData = value } }, actions: { //将传入数据进行全局缓存 changeFormData({ commit }, value) { commit('CHANGE_FORMDATA', value) }, // 重置表单 resetFormData({ commit, state }, value) { const obj = Object.assign({}, state.formData) for (const key in obj) { obj[key] = undefined } commit('CHANGE_FORMDATA', obj) } } } export default xxx
2.将第一步的js在modules.js 进行全局注册
import product from '@/xxx/xxx' const modules = { xxx } export default modules
3.在需要的页面调用
import { mapState } from 'vuex'
computed: { ...mapState({ storeFormData: state => state.xxx.formData }) },调用changeFormData缓存数据,或调用resetFormData清空缓存的数据
this.$store.dispatch('xxx/changeFormData', this.formData)
this.$store.dispatch('xxx/resetFormData')