vue 无法获取到template中的dom元素?

作者站长头像
站长
· 阅读数 9

我希望能够获取<template #loadMore>中 id为"is_load"的div元素但是ref无法引用到 ,在onMounted函数中使用nextTick回调打印处理这个dom也是空的,等待渲染后也不行有什么方法可以操作到dom

 <a-list
          class="demo-loadmore-list"
          :loading="initLoading"
          item-layout="horizontal"
          :data-source="list"
          :locale="locale"
      >
        <template #loadMore>
          <div id="is_load" ref="test"
              v-if="!initLoading && !loading"
              :style="{ textAlign: 'center', marginTop: '12px', height: '32px', lineHeight: '32px' }"
          >
            <a-button @click="onLoadMore">加载更多</a-button>
          </div>
        </template>

        <!--     商品item信息-->
        <template #renderItem="{ item }">
          <a-card hoverable  class="custom-card">
            <template #cover>
              <img alt="example" :src="JSON.parse(item.imageUrl)[0]" class="item_image"/>
            </template>
            <a-card-meta :title="item.price+' 元'" style="text-align: center;">
              <template #description>{{ item.goodsName }}</template>
            </a-card-meta>
          </a-card>
        </template>
      </a-list>
<script lang="ts" setup>
import {onMounted, ref, nextTick} from 'vue';
const test=ref();
onMounted( () => {
      console.log(test.value)  //打印为空
  });
document.getElementById("is_load")  //也没有拿到这个元素
</script>

vue 无法获取到template中的dom元素?

回复
1个回答
avatar
test
2024-06-21
v-if="!initLoading && !loading"

你的 initLoadingloading 数据在渲染完成后是什么情况,如果这个条件是 false 那确实没有渲染出来呀

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容