[英]How can i do a setter to fix computed propery problem - VueJS
Im trying to fix this error where i cant close the dialog by clicking the X button on the top right, but i can close it by clicking the "Agregar" or "Cancelar" button.我试图修复这个错误,我无法通过单击右上角的 X 按钮来关闭对话框,但我可以通过单击“Agregar”或“Cancelar”按钮来关闭它。
This is how i sync my Dialog这就是我同步对话框的方式
<el-dialog title="Agregar Persona" :visible.sync="getAgregarPersonaDialog">
And here is my Computed Property这是我的计算属性
computed: {
getAgregarPersonaDialog() {
return this.$store.state.agregarPersonaDialog;
}
}
This is how i change the value of my state这就是我更改 state 值的方式
setAgregarPersonaDialogo(state) {
state.agregarPersonaDialog = !state.agregarPersonaDialog
},
And in my state i have this atribute在我的 state 我有这个属性
export default new Vuex.Store({
state: {
agregarPersonaDialog: false
}
});
This is the error i have everytime i click the X button on the dialog这是我每次单击对话框上的 X 按钮时遇到的错误
Computed property "getAgregarPersonaDialog" was assigned to but it has no setter.
ti change the state you need
1. action
2. Mutator
example in store.js
state: {
agregarPersonaDialog: false,
},
// value is data what you send from your componet
action: {
changeState(context){
context.commit('changeValue')
},
mutations: {
changeValue(state) {
state.agregarPersonaDialog = !state.agregarPersonaDialog;
}
},
---------------------
in your compoent create method
methods: {
changeValue(){
this.$store.dispatch('changeState');
}
}
try this!尝试这个!
When using Vuex, if your need to mutate any data on state arises then you absolutely need an action that calls a mutation to mutate state.使用 Vuex 时,如果您需要对 state 上的任何数据进行变异,那么您绝对需要一个调用变异的操作来变异 state。
This is the most important use case of Vuex, to keep track of actions that mutates state这是 Vuex 最重要的用例,用于跟踪改变 state 的操作
what you did is absolutely wrong.你所做的绝对是错误的。
To achieve your goal, you need two things.为了实现你的目标,你需要两件事。
action: {
// The first parameter is an object, so it be destructured
// for simple access
doSomething({ commit }, payload){
// commit a change by calling mutation, second parameter
// is the arguments you passed to your action
commit('mutateData', payload)
}
},
mutations: {
mutateData(state, payload) {
// You can instead commit the argument
// I.e state.data = payload
state.agregarPersonaDialog = !state.agregarPersonaDialog;
}
},
You can then dispatch your actions in any components然后,您可以在任何组件中调度您的操作
methods: {
changeValue(){
this.$store.dispatch('doSomething', 'this is an arg');
// Note I passed an argument just for illustration
// in your case, you don't have to
}
}
Now your code should work现在你的代码应该可以工作了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.