📂Vue
🔖Vue
2023-11-30 11:42阅读: 10评论: 0推荐: 0

ref回调形式的模版引用

2023年11月30日11:41:02

ref回调形式的模版引用

掉坑里,记录下。v-for 循环拿取 ref 模版引用两种保险方式

<template>
  <a-form>
    <a-form-item v-for='(f, idx) in formField' :key='idx'>
      <template #label>
        <label>{{ f.title }}</label>
      </template>
      <a-form-item>
        <component :is='xxx' :ref='(el) => componentRefs.push(el)' />
      </a-form-item>
    </a-form-item>
  </a-form>
</template>

<script lang='ts' setup>
import { ref, onMounted } from 'vue';

const componentRefs = ref<any[]>([]);

onMounted(() => {
  console.log(componentRefs.value)
})
</script>
<template>
  <a-form>
    <a-form-item v-for='(f, idx) in formField' :key='idx'>
      <template #label>
        <label>{{ f.title }}</label>
      </template>
      <a-form-item>
        <component :is='xxx' :ref='funcRef' />
      </a-form-item>
    </a-form-item>
  </a-form>
</template>

<script lang='ts' setup>
import { ref, onMounted } from 'vue';

const componentRefs = ref<any[]>([]);

onMounted(() => {
  console.log(componentRefs.value)
})

function funcRef (el) {
  if(el)
    componentRefs.push(el)
}
</script>

本文作者:邪妖怪

本文链接:https://www.cnblogs.com/lastkiss/p/17866977.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   邪妖怪  阅读(10)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起