9 - Lifecycle and Template Refs
生命周期和模板引用vuejs.orgvuejs.org/tutorial
为什么需要
Vue 为我们处理了所有的 DOM 更新,这要归功于响应性和声明式渲染。然而,有时我们也会不可避免地需要手动操作 DOM。
这时我们需要使用模板引用——也就是指向模板中一个 DOM 元素的 ref。我们需要通过这个特殊的 ref attribute 来实现模板引用:
Lifecycle hook
这是因为当 <script setup> 执行时,DOM 元素还不存在。模板引用 ref 只能在组件挂载后访问。
要在挂载之后执行代码,我们可以使用 onMounted() 函数:
它允许我们注册一个在组件的特定生命周期调用的回调函数
Example 1
<script setup>
import { ref } from 'vue'
const pElementRef = ref(null)
</script>
<template>
<p ref="pElementRef">Hello</p>
</template>Answer
<script setup>
import { ref, onMounted } from 'vue'
const pElementRef = ref(null)
onMounted(() => {
pElementRef.value.textContent = 'mounted!'
})
</script>
<template>
<p ref="pElementRef">Hello</p>
</template>Example 2
完成一个组件,要求:
- 挂载后(
onMounted):- 让
<p ref="pRef">的文本变成 “Ready”,并设置为绿色。 - 让
<input ref="iptRef">自动获得焦点。 - 启动一个
setInterval,每 1s 在<p>文本末尾追加一个点"."。
- 点击按钮时:
- 把
<p>的文本改为当前时间字符串。 - 同时把
count加一(用于演示onUpdated)。
- 更新后(
onUpdated):- 把
<p>的title属性更新为Updated {count}。
- 卸载时(
onUnmounted):- 清理定时器。
<!-- Example.vue -->
<script setup>
import { ref, onMounted, onUpdated, onUnmounted } from 'vue'
const pRef = ref(null) // TODO: 初始化模板引用
const iptRef = ref(null) // TODO: 初始化模板引用
const count = ref(0)
let timerId = null
onMounted(() => {
// 1) 修改 <p> 的文本与颜色;让 <input> 聚焦;启动定时器每秒在 <p> 末尾追加 "."
const p = pRef.value // TODO
const ipt = iptRef.value // TODO
// TODO: 设置 p 的文本为 "Ready" 并改为绿色
// TODO: 让输入框聚焦
// TODO: 启动 setInterval 每秒在 p 的文本末尾追加 "."
})
onUpdated(() => {
// 2) 每次响应式更新后,为 <p> 设置 title="Updated {count}"
const p = pRef.value
// TODO: 设置 p.title
})
onUnmounted(() => {
// 3) 清理定时器
// TODO: clearInterval
})
function onClick() {
// 点击后将 <p> 文本改为当前时间字符串,并使 count 自增
const p = pRef.value
// TODO: 设置 p.textContent 为 new Date().toLocaleTimeString()
// TODO: count 自增
}
</script>
<template>
<div class="box">
<p ref="pRef">loading...</p>
<input ref="iptRef" placeholder="我会在挂载后自动获得焦点" />
<button @click="onClick">更新时间并计数</button>
<div>count: {{ count }}</div>
</div>
</template>
<style scoped>
.box { padding: 12px; border: 1px solid #ddd; border-radius: 8px; }
</style>
Answer
<!-- Example.vue -->
<script setup>
import { ref, onMounted, onUpdated, onUnmounted } from 'vue'
const pRef = ref(null)
const iptRef = ref(null)
const count = ref(0)
let timerId = null
onMounted(() => {
const p = pRef.value
const ipt = iptRef.value
if (!p || !ipt) return
// 设置 <p> 文本与颜色
p.textContent = 'Ready'
p.style.color = 'green'
// 让输入框聚焦
ipt.focus()
// 每秒在 <p> 文本末尾追加 "."
timerId = setInterval(() => {
p.textContent = (p.textContent || '') + '.'
}, 1000)
})
onUpdated(() => {
const p = pRef.value
if (p) {
p.title = `Updated ${count.value}`
}
})
onUnmounted(() => {
if (timerId) {
clearInterval(timerId)
timerId = null
}
})
function onClick() {
const p = pRef.value
if (p) {
p.textContent = new Date().toLocaleTimeString()
}
count.value += 1
}
</script>
<template>
<div class="box">
<p ref="pRef">loading...</p>
<input ref="iptRef" placeholder="我会在挂载后自动获得焦点" />
<button @click="onClick">更新时间并计数</button>
<div>count: {{ count }}</div>
</div>
</template>
<style scoped>
.box { padding: 12px; border: 1px solid #ddd; border-radius: 8px; }
</style>