P粉5881526362023-08-28 00:54:33
Using the Combination API
Simple solution for tracking using extra references
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 |
Track category activity if you want to implement multiple selection capabilities
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,关闭其他类别 |