简体   繁体   English

我如何做一个设置器来解决计算的属性问题 - VueJS

[英]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.为了实现你的目标,你需要两件事。

  1. an action一种行为
  2. a mutation突变
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.

相关问题 如何在将 element-ui 与 VueJs 一起使用时在控制台中消除此错误“计算属性 validateState 已分配给但它没有设置器” - How do I get rid of this error "Computed property validateState was assigned to but it has no setter" in the console while using element-ui with VueJs 如何解决分配给计算属性“搜索”但没有设置器的问题? - How can I solve Computed property "search" was assigned to but it has no setter? 如何在 Vuejs 中改进这个计算? - How can this computed be improved in Vuejs? 如果计算属性vuejs中没有任何更改,如何将旧值传递给setter? - How to pass old value to setter if nothing is changed in computed property vuejs? VueJS 如何在 v-for 中使用计算属性 - VueJS How can I use computed property with v-for 如何通过Vuejs中的代码调用计算属性? - How can I invoke computed property through code in Vuejs? 如何使用 jest 在 vuejs 中测试计算属性? - how can i test a computed property in vuejs using jest? 如何修复 VueJS 中的“这是未定义的”? - How can i fix “this is undefined” in VueJS? Vuejs,带有setter冻结组件的计算属性 - Vuejs, computed property with setter freeze component 如何使用计算属性过滤VueJS中v-for循环的结果? - How do I filter the results of a v-for loop in VueJS with a computed property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM