Vue 复选框过滤器组件不起作用
Vue Checkbox filter component not working
在单个文件组件中,我制作了一个复选框数组:
<label
v-for="(category, index) in categories"
:key="index"
>
<input type="checkbox" :value="category.value"
@change="emitSearchValue">
<span class="ml-2 text-gray-700 capitalize">{{ category.value }}</span>
</label>
我使用 EventBus 将数据传输到 List 组件:
methods: {
emitSearchValue() {
EventBus.$emit('checked-value', 'this.checkedCategories')
}
}
在 List 组件中,我监听 EventBus:
created() {
EventBus.$on('checked-value', (checkedCategories) => {
this.checkedvalue = checkedCategories;
})
}
然后我计算的 属性 看起来像这样:
computed: {
filteredData() {
return this.blogs.filter(blog =>
// if there are no checkboxes checked. all values will pass otherwise the category must be included
!this.checkedCategories.length || this.checkedCategories.includes(blog.category)
)
}
},
在控制台中我得到:
[Vue warn]: Error in render: "TypeError: Cannot read property 'length' of undefined"
这里是 link 到 sandbox。
这里有什么问题吗?
将计算属性 filteredData
更改为:
computed: {
filteredData() {
if(!this.checkedvalue.length) return this.experiences
return this.experiences.filter(experience =>
this.checkedvalue.includes(experience.category)
);
}
}
在单个文件组件中,我制作了一个复选框数组:
<label
v-for="(category, index) in categories"
:key="index"
>
<input type="checkbox" :value="category.value"
@change="emitSearchValue">
<span class="ml-2 text-gray-700 capitalize">{{ category.value }}</span>
</label>
我使用 EventBus 将数据传输到 List 组件:
methods: {
emitSearchValue() {
EventBus.$emit('checked-value', 'this.checkedCategories')
}
}
在 List 组件中,我监听 EventBus:
created() {
EventBus.$on('checked-value', (checkedCategories) => {
this.checkedvalue = checkedCategories;
})
}
然后我计算的 属性 看起来像这样:
computed: {
filteredData() {
return this.blogs.filter(blog =>
// if there are no checkboxes checked. all values will pass otherwise the category must be included
!this.checkedCategories.length || this.checkedCategories.includes(blog.category)
)
}
},
在控制台中我得到:
[Vue warn]: Error in render: "TypeError: Cannot read property 'length' of undefined"
这里是 link 到 sandbox。
这里有什么问题吗?
将计算属性 filteredData
更改为:
computed: {
filteredData() {
if(!this.checkedvalue.length) return this.experiences
return this.experiences.filter(experience =>
this.checkedvalue.includes(experience.category)
);
}
}