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,关闭其他类别 |