前端学习-vue学习008-生命周期和模板引用

官方教程链接

ref标签(模板引用)

手动操作 DOM,使用模板引用,就是指向模板中一个 DOM 元素的 ref

<p ref="pElementRef">hello</p>

要访问该引用,我们需要声明一个同名的 ref:

const pElementRef = ref(null)

生命周期

详见前端学习-vue视频学习010-生命周期

<template>
  <!-- html -->
  <div class="app">
      <Todo/>
      <p ref="pElementRef">hello</p>
  </div>
</template>

<script lang="ts" setup>
  import Todo from './components/Todo.vue';
  import { ref,onMounted } from 'vue';

  const pElementRef = ref()

  onMounted(() => {
    pElementRef.value.textContent = 'bye'
  })
  
</script>

<style>

</style>

posted @   ayubene  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示