vue3生命周期

1.生命周期定义

每个 Vue 实例在被创建时都要经过一系列的初始化过程。
例如:从开始创建、初始化数据、编译模板、挂载Dom、数据变化时更新DOM、卸载等一系列过程。
我们称 这一系列的过程 就是Vue的生命周期。
通俗说就是Vue实例从创建到销毁的过程,就是生命周期。
同时在这个过程中也会运行一些叫做生命周期钩子的函数,这给了用户在不同阶段添加自己的代码的机会,利用各个钩子来完成我们的业务代码。

钩子函数

钩子:钩子函数可以简单理解为是一种系统在不同的阶断自动执行、用户无须干预的函数。

2.vue3中的生命周期

1.普通写法

Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名:

  • beforeDestroy改名为 beforeUnmount
  • destroyed改名为 unmounted
    vue3:

    lifecycle_2

<template >
  <div>
    <h3>生命周期</h3>
    <button @click="count++">修改count的值---{{count}}</button>
    <p><button @click="del">点击销毁</button> </p>
  </div>
</template>
<script>
import {ref,getCurrentInstance} from "vue";

export default {
  data(){
    return {
        count:0
    }
  },
 setup(props) {
     console.log("setup")
     const instance=getCurrentInstance()
    return {
      instance,
    }
   },
  methods: {
  //销毁
     del(){
      this.instance.root.appContext.app.unmount()
   },
  },
  beforeCreate() {
    console.log("beforeCreate")
  },
  created() {
    //访问setup中的数据
    console.log("count",this.count) 
    console.log("created");
  },
  beforeMount() {
    console.log("beforeMount")
  },
  mounted() {
    console.log("mounted")
  },
  beforeUpdate() {
    console.log("beforeUpdate")
  },
  updated() {
    console.log("updated")
  },
  beforeUnmount() {
    console.log("beforeUnmount");
  },
  unmounted() {
    console.log("unmounted");
  },
}
</script>

2.setup中写生命周期

  • Vue3.0也提供了 Composition API 形式的生命周期钩子,与Vue2.x中钩子对应关系如下:
    • beforeCreate===>setup()
    • created=======>setup()
    • beforeMount ===>onBeforeMount
    • mounted=======>onMounted
    • beforeUpdate===>onBeforeUpdate
    • updated =======>onUpdated
    • beforeUnmount ==>onBeforeUnmount
    • unmounted =====>onUnmounted
选项式 APIHook inside setup
beforeCreateNot needed*
createdNot needed*
beforeMountonBeforeMount
mountedonMounted
beforeUpdateonBeforeUpdate
updatedonUpdated
beforeUnmountonBeforeUnmount
unmountedonUnmounted
errorCapturedonErrorCaptured
renderTrackedonRenderTracked
renderTriggeredonRenderTriggered
activatedonActivated
deactivatedonDeactivated

区别

vue2vue3差异比较
beforeCreatesetupsetup() :开始创建组件之前,在beforeCreate和created之前执行。创建的是data和method
createdsetup
beforeMountonBeforeMount组件挂载到节点上之前执行的函数
mountedonMounted组件挂载完成后执行的函数
beforeUpdateonBeforeUpdate组件更新之前执行的函数
updatedonUpdated组件更新完成之后执行的函数
beforeDestroyonBeforeUnmount卸载之前执行的函数,相比改名了
destroyedonUnmounted卸载之后执行的函数
activatedonActivated被包含在中的组件,会多出两个生命周期钩子函数。被激活时执行
deactivatedonDeactivated比如从 A 组件,切换到 B 组件,A 组件消失时执行
errorCapturedonErrorCaptured当捕获一个来自子孙组件的异常时激活钩子函数
onRenderTrackedvue3新增的周期用于开发调试使用的
onRenderTriggeredvue3新增的周期用于开发调试使用的
  • vue2的beforeCreatecreate变成了setup

  • 除了setup外大部分还是vue2的名字,只是在前面加了个on

  • vue2的destroyedbeforDestroy变成了on

<template>
  <div>
    <button @click="count++">修改count的值---{{count}}</button>
    <p><button @click="del">点击销毁</button> </p>

  </div>
</template>
<script setup>
 import {ref,reactive,getCurrentInstance,onMounted,onBeforeMount,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted} from "vue"
const count=ref(0);
const instance=getCurrentInstance();
const del=()=>{
    instance.root.appContext.app.unmount();
}
onBeforeMount(()=>{
  console.log("onBeforeMount");
})
onMounted(()=>{
console.log("onMounted")
})
onBeforeUpdate(()=>{
console.log("onBeforeUpdate")
})
onUpdated(()=>{
console.log("onUpdated")
})
onBeforeUnmount(()=>{
console.log("onBeforeUnmount")
})
onUnmounted(()=>{
console.log("onUnmounted")
})

</script>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中,生命周期函数的使用有所改变。以下是Vue 3中的生命周期函数及其使用方法: 1. `beforeCreate`:在实例初始化之后、数据观测之前调用。Vue 3中不再推荐使用此生命周期函数,可以使用`setup()` API来替代。 2. `created`:在实例创建完成后调用。Vue 3中不再推荐使用此生命周期函数,可以使用`setup()` API来替代。 3. `beforeMount`:在挂载开始之前调用。此时模板编译已完成,但尚未将模板渲染到页面中。 4. `mounted`:在挂载完成后调用。此时模板已经被渲染到页面中。 5. `beforeUpdate`:在数据更新之前调用,发生在虚拟DOM重新渲染和打补丁之前。 6. `updated`:在数据更新之后调用,发生在虚拟DOM重新渲染和打补丁之后。 7. `beforeUnmount`:在实例销毁之前调用。 8. `unmounted`:在实例销毁之后调用。 需要注意的是,在Vue 3中,`beforeDestroy`和`destroyed`这两个生命周期函数已经被重命名为`beforeUnmount`和`unmounted`。 另外,Vue 3引入了`setup()` API,用于替代Vue 2中的生命周期函数。`setup()`函数是组件内可选的一个函数,它在组件实例化之前执行。你可以在`setup()`函数中进行组件的初始化工作、数据的响应式声明、注册事件等。`setup()`函数可以返回一个对象,该对象将暴露给组件模板使用。 这是Vue 3中生命周期的基本使用方法,希望对你有所帮助。如果有任何进一步的问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值