P粉5881526362023-08-28 00:54:33
使用组合 API
使用额外的引用进行跟踪的简单解决方案
const selectedCategory = ref("") const contentCategories = ref(["category 1", "category 2", "category 3"]); <button v-for="category in contentCategories" @click="handleClick(category)" :class="selectedCategory === category ? 'active' : 'inactive'" :key="category" > {{ category }} </button> handleClick(category) { selectedCategory.value = category |
如果您想要实现多个选择能力,则跟踪类别活动
const contentCategories = ref([ { name: "Category 1", active: false }, ... ]); <button v-for="category in contentCategories" @click="handleClick(category)" :class="category.active ? 'active' : 'inactive'" :key="category" > {{ category }} </button> handleClick(category) { 找到类别然后设置 active = !active,关闭其他类别 |