Error: [vuex] do not mutate vuex store state outside mutation handlers.
代码如下:
数据格式
let shortCutKeyData = [
{
key: 1,
name: this.$t("info.setup.stopRun"),
keyboard: 'ctrl+R'
},
{
key: 2,
name: this.$t("info.setup.stepRun"),
keyboard: 'ctrl + shift + R'
},
{
key: 3,
name: this.$t("info.setup.copy"),
keyboard: 'ctrl+C'
},
{
key: 4,
name: this.$t("info.setup.delCom"),
keyboard: 'ctrl+D'
},
{
key: 5,
name: this.$t("info.setup.compile"),
keyboard: 'ctrl+B'
},
//{ key: 6, name: '多步编译',keyboard: 'ctrl+V' },
]
vue文件中的逻辑
setupDialog.vue
//改变键盘输入
shortCutKeyChange(index,val) {
this.$store.commit('SAVE_SHORTCUTKEY',this.shortCutKeyData);
},
setup.js
//顶部菜单 设置模块
let state = {
//技能路径
skillUrl: '',
//初始化快捷键的数据
shortCutKeyData: [
{ key: 1, name: "运行",keyboard: 'ctrl+R'},
{ key: 2, name: "单步运行",keyboard: 'ctrl + shift + R' },
{ key: 3, name: '复制',keyboard: 'ctrl+C'},
{ key: 4, name: '删除',keyboard: 'ctrl+D' },
{ key: 5, name: '编译',keyboard: 'ctrl+B' },
//{ key: 6, name: '多步编译',keyboard: 'ctrl+V' },
],
//捕获开关
captureSwitchVal: '',
}
const mutations = {
//保存技能的路径
SAVE_SKILL_URL: (state,skill) => {
state.skillUrl = skill;
},
//保存快捷键的数据
SAVE_SHORTCUTKEY: (state,data) => {
state.shortCutKeyData = data;
},
//保存捕获开关的数据
SAVE_CAPTURESWITCH: (state,val) => {
state.captureSwitchVal = val;
}
}
const actions = {
}
export default {
state,
mutations,
actions
}
如果我用上面的方式,每次commit整个数组就会报错,如果我们换一种方式就可以解决当前报错的问题。
解决方案如下:
setupDialog.vue
//保存快捷键的数据
SAVE_SHORTCUTKEY: (state,data) => {
state.shortCutKeyData[data.idx].keyboard = data.keyboard;
},
setup.js
//改变键盘输入
shortCutKeyChange(index,val) {
this.$store.commit('SAVE_SHORTCUTKEY',{idx :index, keyboard:val});
},