Vuex Error: [vuex] do not mutate vuex store state outside mutation handlers.
Error: [vuex] do not mutate vuex store state outside mutation handlers.
Error reason: the only way to change the status in vuex’s store is to submit mutation.
Vuex also provides a strict mode to control whether to prompt non-standard store value modification. After the strict mode is turned on, if the value is modified outside the mutation, it can also be dynamically rendered to the page, but Vue will have a warning prompt.
My usage scenario:
Vuex saves the data requested by the interface uniformly, and I call the interface in mutation to assign data to state.
solution:
The following reference official documents:
Mixing asynchronous calls in mutation will make your program difficult to debug. For example, when you call two mutation with asynchronous callbacks to change the state, how do you know when to call back and which to call back first?That’s why we have to distinguish between these two concepts. In vuex, mutation is a synchronous transaction
Asynchronous requests cannot be made in mutation. Asynchronous requests are placed in actions. Mutation must be a synchronous function!!
The test.js code example in the store file is as follows:
import * as types from './mutation-types';
import api from '../apis/orderList';
export const state = () => ({
test: '111',
defeatList: []
});
export const getters = {};
export const actions = {
changeNum ({ commit }, Num) {
commit('change_num', Num);
},
getDefeatList ({ commit }, params) {
api.getDefeatList(params).then(res => {
if (res && res.data) {
let List = res.data;
commit(types.GET_DEFEATLIST, List);
}
});
}
};
export const mutations = {
change_num (state, Num) {
state.test = Num;
},
[types.GET_DEFEATLIST] (state, List) {
state.defeatList = List;
}
};
Component invocation:
<template>
<a-table
:columns="columns"
:row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
:data-source="defeatList"
:scroll="{ x: 1500 }"
:pagination="paginationProps"
:row-key="record => record.carNumber"
></a-table>
</template>
computed: {
test () {
return this.$store.state.orderlist.test;
},
defeatList () {
return this.$store.state.orderlist.defeatList;
}
},
methods: {
initList () {
let params = {
pageNum: 1,
...this.form
};
this.$store.dispatch("test/getDefeatList", params);
},
}
Read More:
- [Solved] Vuex Error: unknown action type: changeUserInfo
- Can’t resolve ‘ansi-html-community’ & Cannot find module ‘is-docker (vuex-persistedstate Installed)
- [Solved] npm Install vuex Error: npm ERR! code ERESOLVE
- [Solved] React Click the Event to Modify the State Value Error
- error in ./node_modules/@lit/reactive-element/decorators/state.jsModule parse failed: Unexpected
- [Solved] Custom components with click events do not work
- The solution to the problem that the custom styles of UI components such as element-ui in the vue project do not take effect
- Bugly automatically upload script and report zip error: nothing to do! In xcode10
- ERROR Error: [@ant-design/icons-angular]:the icon XXX does not exist or is not registered.
- Ant design vue table Error: h is not defined [How to Solve]
- TypeError: r.indexOf is not a function TypeError: r.indexOf is not a function
- Vue Dynamic Display Picture Error 404: Not Found [How to Solve]
- [Solved] node.js Upload Files Error: Multipart: boundary not found multer
- [Solved] Vue Import swiper.css Error: Module not found…
- How to Solve pinia Error in ts File
- [Solved] Vue uses webpack to package error: Createapp is not a function
- [Solved] Vue Error: Uncaught TypeError: Vue.createApp is not a function
- How to Fix “HTTP 405 method not allowed” Error
- How to Solve “Vue is not defined” Error
- [Solved] Uncaught SyntaxError: The requested module does not provide an export named