锚点效果:当点击el-select的option后,页面滚动到对应区域
DOM节点绑定id变量
<div class="category-title" :id="index">
<h2> {{ index }} </h2>
</div>
** debut:typescript**
注意:绑定id时报错:runtime-dom.d.ts(425, 3): 所需类型来自属性 “id”,在此处的 “ElementAttrs” 类型上声明该属性
虽然有报错,但可以用,原因是index被看作number,而id需要的是string,解决方式是点开runtime-dom.d.ts(425, 3),id?:string之后添加 | number
El-select绑定change
当el-select的v-model值发生改变时触发change事件
<el-select v-model="groupSelected" class="groupSelected" placeholder="主页" style="width: 1rem;" @change="anchorToGroup">
<el-option v-for="(item, index) in useAppStore.category" :key="index" :label="item" :value="item" />
</el-select>
锚点跳转事件anchorToGroup
// 自定义函数:锚点跳转分组
const anchorToGroup = (val:string) => {
console.log('anchorToGroup val:', val)
const domID = `#${val}` // 转换成DOM节点的id值
document.querySelector(domID)?.scrollIntoView(true)
}