如何在 VueX 的 computed/methods 属性 块中包含 if/else 语句?
How to have an if/else statement in a computed/methods property block for VueX?
我将一个变量传递到名为“store”的组件中。这将存储需要在组件中使用的商店的名称。该组件只需要确保从正确的存储中获取数据。
computed: {
if (this.store === "storeNumber1")
...mapGetters(this.store, ["thing1", "thing2"]),
else if(this.store === "storeNumber2")
...mapGetters(this.store, ["thing1", "thing2"]),
}
props: {
store
}
这不起作用。我需要什么才能让这个概念发挥作用?那么 Vue Mutations 呢?谢谢。
是的,这行不通。 computed
属性需要具有简单的函数 return。你想要的东西需要在 computed
和 methods
之间合成:
methods: {
myFunction() {
if (this.store === "storeNumber1") {
return ...mapGetters(this.store, ["thing1", "thing2"])
}
else if(this.store === "storeNumber2") {
return ...mapGetters(this.store, ["thing1", "thing2"])
}
}
}
computed: {
myComputedProperty() {
return this.myFunction()
}
}
props: {
store
}
mapGetters
并不意味着基于 prop
.
以反应方式使用
相反,您应该使用普通语法从 this.$store.getters
对象访问命名空间 getter:
this.$store.getters[__MODULE_NAME__ + '/' + __GETTER_NAME]
在每个 getter:
的计算 属性 中使用该语法
export default {
props: {
store: {
type: String,
required: true,
},
},
computed: {
thing1() {
return this.$store.getters[this.store + '/thing1']
},
thing2() {
return this.$store.getters[this.store + '/thing2']
},
},
}
我将一个变量传递到名为“store”的组件中。这将存储需要在组件中使用的商店的名称。该组件只需要确保从正确的存储中获取数据。
computed: {
if (this.store === "storeNumber1")
...mapGetters(this.store, ["thing1", "thing2"]),
else if(this.store === "storeNumber2")
...mapGetters(this.store, ["thing1", "thing2"]),
}
props: {
store
}
这不起作用。我需要什么才能让这个概念发挥作用?那么 Vue Mutations 呢?谢谢。
是的,这行不通。 computed
属性需要具有简单的函数 return。你想要的东西需要在 computed
和 methods
之间合成:
methods: {
myFunction() {
if (this.store === "storeNumber1") {
return ...mapGetters(this.store, ["thing1", "thing2"])
}
else if(this.store === "storeNumber2") {
return ...mapGetters(this.store, ["thing1", "thing2"])
}
}
}
computed: {
myComputedProperty() {
return this.myFunction()
}
}
props: {
store
}
mapGetters
并不意味着基于 prop
.
相反,您应该使用普通语法从 this.$store.getters
对象访问命名空间 getter:
this.$store.getters[__MODULE_NAME__ + '/' + __GETTER_NAME]
在每个 getter:
的计算 属性 中使用该语法export default {
props: {
store: {
type: String,
required: true,
},
},
computed: {
thing1() {
return this.$store.getters[this.store + '/thing1']
},
thing2() {
return this.$store.getters[this.store + '/thing2']
},
},
}