Vue.js 为什么我在计算属性中得到错误返回?
我正在使用Vue.js 为什么我在计算属性中得到错误返回?,vue.js,vuex,Vue.js,Vuex,我正在使用Vuex,在Getter-Foo函数中我在数组中返回两个值: return[“重试”]或return[“数据结果”,数据],在computed中,我正在检查数组长度并根据结果返回 computed:{ Foo: function(){ const getFoo = this.$store.getters.Foo; if(getFoo.length === 1) { this.existFoo = false
Vuex
,在Getter-Foo函数中
我在数组中返回两个值:
return[“重试”]
或return[“数据结果”,数据]
,在computed中,我正在检查数组长度
并根据结果返回
computed:{
Foo: function(){
const getFoo = this.$store.getters.Foo;
if(getFoo.length === 1) {
this.existFoo = false
return getFoo[0]
}
this.existFoo = true
return getFoo
}
}
但我发现了这个错误,即使阅读其他帖子,我也无法解决它
34:9错误“Foo”计算属性中的意外副作用
vue/在计算属性中没有副作用37:7意外错误 “Foo”计算属性中的副作用 vue/计算属性中无副作用
不允许您更改computeds中的状态。 尝试使用另一个computed而不是
existFoo
computed:{
Foo(){
if(this.$store.getters.Foo.length === 1) {
return this.$store.getters.Foo[0]
}
return this.$store.getters.Foo
},
existFoo(){
return this.$store.getters.Foo.length > 1
}
}
现在,您应该从
状态中删除existFoo
您不允许更改computeds中的状态。
尝试使用另一个computed而不是existFoo
computed:{
Foo(){
if(this.$store.getters.Foo.length === 1) {
return this.$store.getters.Foo[0]
}
return this.$store.getters.Foo
},
existFoo(){
return this.$store.getters.Foo.length > 1
}
}
现在您应该从
状态中删除existFoo
您可以使用观察程序来观察存储值并设置局部变量
计算:{
getFooFromStore(){
返回此。$store.getters.Foo
}
}
观察:{
getFooFromStore:函数(){
this.existFoo=this.getFooFromStore[0]?false:true;
}
}
您可以使用监视程序监视存储值并设置局部变量
计算:{
getFooFromStore(){
返回此。$store.getters.Foo
}
}
观察:{
getFooFromStore:函数(){
this.existFoo=this.getFooFromStore[0]?false:true;
}
}
可能是重复的可能是重复的我没有想到,谢谢我没有想到,一旦我需要访问数据(){existFoo}
并通过计算函数是不可能的,对吗?对不起,我不完全理解你的问题。你可以在计算属性中访问数据变量,但如果你是这个意思的话,你不能在计算属性中设置它们。是的,我不能从计算属性中访问,但是使用watch属性可以设置任何条件。我没有想过,thanksI没有想过,一旦我需要访问数据(){existFoo}
和通过计算函数
是不可能的,对吗?对不起,我不完全理解你的问题。你可以访问计算属性中的数据变量,但如果你是这个意思的话,你不能在计算属性中设置它们。是的,我不能从计算属性中访问,但是使用watch属性可以设置任何条件