uniapp用vue3实现点谁谁高亮
时间: 2024-04-30 08:18:16 浏览: 10
可以通过在模板中绑定class样式来实现。例如,假设你有一个包含列表项的组件,每个列表项都有一个唯一的id和一个名字属性。你可以在模板中绑定一个class样式,根据当前选定的列表项id来判断是否需要高亮显示:
```html
<template>
<div>
<ul>
<li v-for="item in items" :key="item.id" :class="{ 'active': selectedId === item.id }" @click="selectItem(item)">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
],
selectedId: null
}
},
methods: {
selectItem(item) {
this.selectedId = item.id
}
}
}
</script>
<style>
.active {
background-color: yellow;
}
</style>
```
在上面的代码中,我们使用`:class`绑定语法,将一个对象传递给了class属性。这个对象中的键是样式类名,值为一个布尔值,表示是否需要应用这个样式类。当`selectedId`等于当前列表项的id时,我们将`active`这个样式类应用到该列表项上,以实现高亮显示。