问题
In a single file component I made a checkbox array:
<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>
With EventBus I transfer the data to the List component:
methods: {
emitSearchValue() {
EventBus.$emit('checked-value', 'this.checkedCategories')
}
}
In the List component I listen for the EventBus:
created() {
EventBus.$on('checked-value', (checkedCategories) => {
this.checkedvalue = checkedCategories;
})
}
Then my computed property look like this:
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)
)
}
},
In the console I get:
[Vue warn]: Error in render: "TypeError: Cannot read property 'length' of undefined"
Here is a link to a sandbox.
Whats wrong here?
回答1:
Change the computed prop filteredData
to:
computed: {
filteredData() {
if(!this.checkedvalue.length) return this.experiences
return this.experiences.filter(experience =>
this.checkedvalue.includes(experience.category)
);
}
}
来源:https://stackoverflow.com/questions/62413901/vue-checkbox-filter-component-not-working