您现在的位置是:首页 >技术交流 >vue3生命周期函数介绍以及使用网站首页技术交流
vue3生命周期函数介绍以及使用
简介vue3生命周期函数介绍以及使用
生命周期函数介绍以及使用
在 Vue 3 中,生命周期函数的使用方式与 Vue 2 有所不同。Vue 3 推荐使用
主要生命周期函数及其用途
生命周期钩子(Vue 3) | 对应 Vue 2 生命周期钩子 | 用途 |
---|---|---|
onBeforeMount | beforeMount | 在组件挂载到 DOM 之前调用,此时模板编译已经完成,但还未插入到页面中。常用于一些初始化操作,比如设置一些初始状态,但不需要访问 DOM。 |
onMounted | mounted | 组件已经挂载到 DOM 之后调用,此时可以访问到真实的 DOM 元素。常用于执行一些依赖于 DOM 的操作,比如获取元素的尺寸、绑定事件监听器等。 |
onBeforeUpdate | beforeUpdate | 在组件数据发生变化,虚拟 DOM 重新渲染和打补丁之前调用。可以用于在更新之前获取旧的 DOM 状态。 |
onUpdated | updated | 在组件数据更新,虚拟 DOM 重新渲染和打补丁之后调用。可以用于在更新后对 DOM 进行一些操作,比如更新滚动位置等。 |
onBeforeUnmount | beforeDestroy (Vue 2.x 中) / beforeUnmount (Vue 2.7+) | 在组件实例被卸载之前调用。可以用于清理一些副作用,比如清除定时器、取消网络请求等。 |
onUnmounted | destroyed (Vue 2.x 中) / unmounted (Vue 2.7+) | 在组件实例被卸载之后调用。此时组件已经完全销毁,所有的事件监听器和子实例也已经被销毁。 |
onErrorCaptured | errorCaptured | 当捕获到一个来自子孙组件的错误时调用。可以用于记录错误信息、显示错误提示等。 |
onRenderTracked | 无直接对应 | 调试钩子,当响应式依赖被跟踪时调用。用于调试响应式系统的依赖收集过程。 |
onRenderTriggered | 无直接对应 | 调试钩子,当响应式依赖被触发时调用。用于调试响应式系统的更新触发过程。 |
代码演示
<template>
<div>
<h1>{{ message }}</h1>
<button @click="updateMessage">更新消息</button>
</div>
</template>
<script setup>
import { ref, onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, onUnmounted } from 'vue';
// 定义响应式数据
const message = ref('初始消息');
// 模拟一个定时器
let timer;
// onBeforeMount 钩子
onBeforeMount(() => {
console.log('onBeforeMount: 组件即将挂载');
});
// onMounted 钩子
onMounted(() => {
console.log('onMounted: 组件已挂载');
// 在组件挂载后启动一个定时器
timer = setInterval(() => {
console.log('定时器在运行...');
}, 1000);
});
// onBeforeUpdate 钩子
onBeforeUpdate(() => {
console.log('onBeforeUpdate: 组件数据即将更新');
});
// onUpdated 钩子
onUpdated(() => {
console.log('onUpdated: 组件数据已更新');
});
// onBeforeUnmount 钩子
onBeforeUnmount(() => {
console.log('onBeforeUnmount: 组件即将卸载');
// 在组件卸载前清除定时器
clearInterval(timer);
});
// onUnmounted 钩子
onUnmounted(() => {
console.log('onUnmounted: 组件已卸载');
});
// 更新消息的方法
const updateMessage = () => {
message.value = '更新后的消息';
};
</script>
<style scoped>
/* 样式部分 */
</style>
代码解释
onBeforeMount
:在组件挂载到 DOM 之前触发,这里只是简单打印一条日志。onMounted
:组件挂载到 DOM 后触发,启动了一个定时器,每秒打印一条日志。onBeforeUpdate
:在组件数据更新之前触发,可用于在更新前获取旧的状态。onUpdated
:组件数据更新完成后触发,可用于在更新后对 DOM 进行操作。onBeforeUnmount
:在组件卸载之前触发,清除了之前启动的定时器,避免内存泄漏。onUnmounted
:组件卸载完成后触发,此时组件已经完全销毁。
在选项式 API 中使用生命周期函数
如果你不使用
<template>
<div>
<h1>{{ message }}</h1>
<button @click="updateMessage">更新消息</button>
</div>
</template>
<script>
export default {
data() {
return {
message: '初始消息'
};
},
beforeMount() {
console.log('beforeMount: 组件即将挂载');
},
mounted() {
console.log('mounted: 组件已挂载');
this.timer = setInterval(() => {
console.log('定时器在运行...');
}, 1000);
},
beforeUpdate() {
console.log('beforeUpdate: 组件数据即将更新');
},
updated() {
console.log('updated: 组件数据已更新');
},
beforeUnmount() {
console.log('beforeUnmount: 组件即将卸载');
clearInterval(this.timer);
},
unmounted() {
console.log('unmounted: 组件已卸载');
},
methods: {
updateMessage() {
this.message = '更新后的消息';
}
}
};
</script>
<style scoped>
/* 样式部分 */
</style>
以上就是 Vue 3 中生命周期函数的详细介绍及代码演示,你可以根据具体需求在合适的生命周期钩子中执行相应的操作。
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。