遇到问题:Vue3中动态的ref怎么使用?
我在渲染评论列表的时候,每个列表是不一样的,所以我要拿到ref。然后调用里面的方法。
平时是怎么玩的呢?
比如说一个LoadingView,ref为loadingView
<template>
<div>
<LoadingView ref="loadingView"><LoadingView>
</div>
</template>
<script setup>
import {ref} from 'vue'
const loadingView = ref(null);
//加载完以后我就可以调用loadingView.loading()方法了。
</script>
那如果是多个呢?比如说我的评论列表,每一个动态的评论都不一样的。
按我们以前的写法
<template>
<div v-for="(item,index) in moyuList" :key="index">
<CommentView :ref="(el)=>if(el)commentViewList[index]=el"><CommentView>
</div>
</template>
<script setup>
import {ref} from 'vue'
const commentViewList = ref([]);
//当我某条动态点击的时候,我要去加载评论列表,我就可以commentViewList[index].value,调用加载评论列表的方法了。
</script>