【发布时间】:2019-11-17 22:49:50
【问题描述】:
我已经为类似 mattrialdesign 的开关构建了一个小组件。可以使用“活动”属性设置开关的状态。为了切换状态,我使用计算值,因为无法更改道具。
如果我将组件标签与硬编码的“活动”道具一起使用,它就可以工作。 如果我使用 vuex getter 来设置 prop,则 prop get 是正确的值,但计算结果是“未定义”。
这里是组件:
<template>
<div class="flex items-center">
<div class="flex h-6 rounded-full shadow w-12 cursor-pointer" :class="status" @click="toggle">
<div class="flex h-6 rounded-full shadow w-6 bg-blue-500">
<slot></slot>
</div>
</div>
<label class="m-0 ml-2 text-lg normal-case">{{label}}</label>
</div>
</template>
<script>
export default{
props:{
active: Boolean,
label: String
},
data() {
return { isActive: this.active }
},
computed:{
status(){
return this.isActive ? 'justify-end bg-success' : 'justify-start bg-primary';
}
},
methods:{
toggle(){
this.isActive = !this.isActive;
},
}
}
</script>
这行得通:
<toggle label="Foo" active="true">Foo</toggle>
这不是:
<toggle label="Foo" :active="this.$store.getters.FooStatus">Foo</toggle>
在 Vue Devtools 中,我可以看到 active 是 true/false,具体取决于 FooStatus,但 isActive 是未定义的。
【问题讨论】:
标签: vuejs2 vue-component vuex