点击按钮新增条件时,新增的内容会展示在最后,需要鼠标滑动才可以看到,所以想要实现点击新增条件时,页面直接跳转到新增内容。
主要代码如下:
<NButton type="primary" size="small" @click="addCondition">新增条件</NButton>
<NForm v-if="conditions.length > 0" ref="formRef2" :model="conditions" :rules="conditionsRules">
<div v-for="(condition,index) in conditions" :key="index" :ref="(el) => setConditionRef(el, index)">
<!-- 实际业务需求-->
</div>
</NForm>
const conditionRefs = ref([])
// 收集动态生成的 div 引用
const setConditionRef = (el, index) => {
if (el) conditionRefs.value[index] = el
}
const addCondition = async () => {
const newCondition = {
// 根据实际需求
};
conditions.value.push(newCondition);
nextTick(() => {
const lastIndex = conditions.value.length - 1
conditionRefs.value[lastIndex]?.scrollIntoView({
behavior: 'auto',
block: 'center'
})
})
}