欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 【Vue】关于Vue3的生命周期

【Vue】关于Vue3的生命周期

2025/2/23 7:08:37 来源:https://blog.csdn.net/qq_57567877/article/details/137700072  浏览:    关键词:【Vue】关于Vue3的生命周期

目录

Vue3中新增了一个setup生命周期函数:(1) setup执行的时机是在beforeCreate生命周期函数之前执行,在setup函数中是不能通过this来获取实例的;(2) 为了命名的统一性,将beforeDestroy 改名为 beforeUnmount,destroyed 改名为 unmounted

在这里插入图片描述
生命周期函数

setup —— 不能通过this来获取实例
beforeCreate —— 建议使用setup来代替
created —— 建议使用setup来代替
beforeMount
mounted
beforeUpdate
updated
beforeUnmount
unmounted

新增加的生命周期函数(在setup中使用)可以在函数前加 on 来访问组件的生命周期

<script>import {onBeforeMount,onMounted,onBeforeUnmount,onUnmounted} from 'vue'export default {name:"AboutView",//生命周期钩子(新语法)setup(){console.log("setup------------------1");onBeforeMount(()=>{console.log("---onBeforeMount---")});onMounted(()=>{console.log("---onMounted---")});onBeforeUnmount(()=>{console.log("---onBeforeUnmount---")});onUnmounted(()=>{console.log("---onUnmounted---")})},//外面这些生命周期,旧语法虽然vue3也支持,感觉可以不用写了;只需要把setup中的钩子给它挂进去就已经够用了beforeCreate() {console.log("beforeCreate-----------------2")},created() {console.log("created-----------------3")},beforeMount() {console.log("beforeMount-----------------4")},mounted() {console.log("mounted-----------------5")},beforeUnmount() {console.log("beforeUnmount-----------------6")},unmounted() {console.log("unmounted-----------------7")},}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词