我正在尝试构建一个创建过滤器按钮的组件,然后通过事件总线发送过滤器对象中的类型属性,以便在我的应用程序的其他地方处理。但是,当我在数据部分添加对象(过滤器)数组时,单击按钮时出现 this.filter is not defined 错误。
我想将过滤器数组保留在此组件中,因为我还试图将活动类动态更改为已单击的任何按钮。
我错过了与道具有关的东西吗?当数据和 v-for 在另一个组件上时,为什么我无法显示按钮?为了解决这个问题,我一直在问自己这些问题。
<template>
<div>
<button
v-for="(filter, index) in filters"
:key="index"
:class="{ active: index === activeItem }"
@click="emitFilter(), selectItem(index)"
:filter="filter"
>
{{ filter.name }}
</button>
</div>
</template>
<script>
import EventBus from '@/components/EventBus'
export default {
props: {
filter: { type: String }
},
data() {
return {
activeItem: 0,
filterResult: '',
filters: [
{ name: 'All', type: 'all' },
{ name: 'Holidays', type: 'holiday' },
{ name: 'Seasons', type: 'season' },
{ name: 'Events', type: 'custom' }
]
}
},
methods: {
emitFilter() {
this.filterResult = this.filter
EventBus.$emit('filter-catagories', this.filterResult)
},
selectItem(index) {
this.activeItem = index
}
}
}
</script>
我的按钮组件用于过滤器组件
<template>
<div>
<span>filters</span>
<FilterBtn />
</div>
</div>
</template>
<script>
import FilterBtn from '@/components/FilterBtn'
export default {
components: {
FilterBtn
}
// data() {
// return {
// filters: [
// { name: 'All', type: 'all' },
// { name: 'Holidays', type: 'holiday' },
// { name: 'Seasons', type: 'season' },
// { name: 'Events', type: 'custom' }
// ]
// }
// }
}
</script>
如您所见,注释部分是我最初拥有过滤器的地方,但我不得不将它们移至按钮组件以添加活动类。
拉风的咖菲猫
白板的微信
陪伴而非守候
回首忆惘然
相关分类