当前位置: 首页 > news >正文

Vue3 生命周期

如下是Vue3的生命周期函数图: 

一、Vue2生命周期和Vue3声明周期的区别

1.  Vue2 中,只要创建Vue实例对象而不需要挂载就可以实现beforeCreate 和 created 生命周期函数。

Vue3中必须要将Vue实例对象挂载完成,所有的准备工作做完,才可以开始实现beforeCreate 和 created 生命周期函数。

2.  Vue2 我们销毁组件,触发的是beforeDestroy 和 destroyed 函数。

在Vue3中,我们销毁组件变为了卸载组件,实现的功能是一致的,不过只是称呼变了,触发的生命周期函数为beforeUnmount 和 unmounted。

二、Vue3生命周期的使用

(一)以配置项的形式调用

和Vue2中调用生命周期函数的方法一致,注意是与setup函数平级。

如下生命周期函数有不懂的,可以参考这篇文章:

Vue生命周期-CSDN博客

export default {name: "MyItem",setup() {},beforeCreate() {console.log("beforeCreate生命周期函数");},created() {console.log("created生命周期函数");},beforeMount() {console.log("beforeMount生命周期函数");},mounted() {console.log("mounted生命周期函数");},beforeUpdate() {console.log("beforeUpdate生命周期函数");},updated() {console.log("update生命周期函数");},beforeUnmount() {console.log("beforeUnmount生命周期函数");},unmounted() {console.log("unmounted生命周期函数");},
};

(二)以composition API 形式调用

以上的生命周期函数和composition API 对应形式如下:

beforeCreate ----> setup()

created ----> setup()

beforeMount ----> onBeforeMount

mounted ----> onMounted

beforeUpdate ----> onBeforeUpdate

updated ----> onUpdated

beforeUnmount ----> onBeforeUnmount

unmounted ----> onUnmounted

使用以上composition API 之前需要先对要使用的API按需引入:

 import { onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, onUnmounted } from "vue";

语法格式如下:

API (() => {  // 内部执行内容  })

import { onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, onUnmounted } from "vue";export default {name: "MyItem",setup() {onBeforeMount(() => {console.log("beforeMount生命周期函数");})onMounted(() => {console.log("mounted生命周期函数");})onBeforeUpdate(() => {console.log("beforeUpdate生命周期函数");})onUpdated(() => {console.log("update生命周期函数");})onBeforeUnmount(() => {console.log("beforeUnmount生命周期函数");})onMounted(() => {console.log("unmounted生命周期函数");})}
};
http://www.lryc.cn/news/233548.html

相关文章:

  • rocketmq 安装dashboard1.0.0 mq消息控制台安装 rocketmq控制台安装 rocketmq-dashboard-1.0.0编译安装
  • 常见的数据结构有哪些?
  • Spring中有哪几种方法获取HttpSession对象
  • springboot开启Redis缓存支持
  • 2.4 矩阵的运算法则
  • 让文字在盒子中水平居中与垂直居中
  • 聊一聊前端面临的安全威胁与解决对策
  • 【matlab学习】现代控制
  • Debezium报错处理系列之九十九:ConnectException: Source offset ‘file‘ parameter is missing
  • 基于深度学习的活体人脸识别检测算法matlab仿真
  • Angular 由一个bug说起之二:trackBy的一点注意事项
  • 单片机FLASH下载算法的制作
  • [nlp] 损失缩放(Loss Scaling)loss sacle
  • Django框架之视图层
  • 商城免费搭建之java商城 java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
  • AI机器学习实战 | 使用 Python 和 scikit-learn 库进行情感分析
  • CANoe-Logging模块如何抓取总线数据
  • Unity中Shader的矩阵加减法
  • IIC总线概述和通信时序代码详细图文解析
  • EtherCAT 伺服控制功能块实现
  • 如何基于OpenCV和Sklearn算法库开展机器学习算法研究
  • 在 Node.js 中发出 HTTP 请求的 5 种方法
  • pipeline agent分布式构建
  • MySQL(17):触发器
  • 挖掘PostgreSQL事务的“中间态”----更加严谨的数据一致性?
  • 多种方法实现conda环境迁移
  • C++ string类(一)
  • 系统时间和JVM的Date时间不一致问题解决
  • 23111701[含文档+PPT+源码等]计算机毕业设计javaweb点餐系统全套餐饮就餐订餐餐厅
  • RabbitMQ 部署及配置详解(集群部署)