[英]Vuex getter is undefined but state appears in console
I have a slightly strange problem .. I am not able to return a specific property in my object that is in the "state" of vuex.我有一个有点奇怪的问题.. 我无法在我的对象中返回处于 vuex 的“状态”的特定属性。
To have a better understanding, I'll put a snippet of my store below:为了更好地理解,我将在下面放一段我的商店:
state: {
activeIdentifier: '', //controlar qual modulo que esta ativo
configListActive: { entity: {} }, //controla qual instancias que esta ativa na visao
listController: {}, //instancias dos controllers
filterParams:{}, //params de filtros feitos
},
getters: {
configListActive: state => param =>{
console.log("State:", state.configListActive)
console.log("Param:", param)
console.log("Result:", state.configListActive[param])
return state.configListActive[param];
},
filterParams: state => param => {
return state.filterParams[param];
},
listController: state => param => {
return state.listController[param];
}
},
[....]
I'm passing a parameter in the getters "configListActive", and my object "configListActive" exists the property, however, the return is "undefined", I will put below the print of the debug that I made.我在获取器“configListActive”中传递了一个参数,并且我的对象“configListActive”存在该属性,但是,返回是“未定义”,我将放在我所做的调试的打印下方。
What could have been done wrong?可能做错了什么?
You've run into one of Vue's change detection caveats .您遇到了 Vue 的变更检测警告之一。 From the docs:
从文档:
Vue cannot detect property addition or deletion.
Vue 无法检测到属性添加或删除。 Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the data object in order for Vue to convert it and make it reactive... However, it's possible to add reactive properties to a nested object using the
Vue.set(object, propertyName, value)
method由于 Vue 在实例初始化期间执行 getter/setter 转换过程,因此数据对象中必须存在一个属性,以便 Vue 对其进行转换并使其具有反应性……但是,可以使用
Vue.set(object, propertyName, value)
方法
From within a component you can use:在组件中,您可以使用:
this.$set(object, propertyName, value)
If the caveat was encountered in Vuex, you can import Vue into the Vuex module:如果在 Vuex 中遇到警告,您可以将 Vue 导入 Vuex 模块:
import Vue from 'vue';
and use Vue.set
in a mutation like:并在突变中使用
Vue.set
,例如:
Vue.set(state.object, property, value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.