思路
根据菜单项的ref中的isChildrenSelected(选中标识)判断是否选中来切换图标
模板切换核心代码
使用v-show会更流畅
<template #icon v-if="getIcon(item)">
<img
:src="getIcon(item, 'active')"
v-show="getRef(index)?.isChildrenSelected"
class="w-4 h-4"
/>
<img
:src="getIcon(item, 'normal')"
v-show="!getRef(index)?.isChildrenSelected"
class="w-4 h-4"
/>
</template>
业务逻辑核心代码
为每一个项命名一个独立的ref名字,使用refName来实现,然后在封装获取某个项的isChildrenSelected是否为true的函数。
const refName = (index: number) => {
const name = `xxxx${index}`
refs.value[index] = name
return name
}
const getRef = (index: number) => {
const instance: any = getCurrentInstance()
return instance.refs[refs.value[index]]
}