• Vue3 生命周期新写法


    介绍

    在Vue3中生命周期有了新的写法,下面来看看吧

    效果

    我们可以看到声明周期函数都被调用了

    在这里插入图片描述

    代码

    // beforeMount => onBeforeMount
    // mounted => onMounted
    // beforeUpdate => onBeforeUpdate
    // beforeUnmount => onBeforeUnmount
    // unmouted => onUnmounted

    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>lesson 40title>
      <script src="https://unpkg.com/vue@next">script>
    head>
    <body>
      <div id="root">div>
    body>
    <script>
    
      const app = Vue.createApp({
        // beforeMount => onBeforeMount
        // mounted => onMounted
        // beforeUpdate => onBeforeUpdate
        // beforeUnmount => onBeforeUnmount
        // unmouted => onUnmounted
        setup() {
          const {
            ref, onBeforeMount, onMounted, onBeforeUpdate, onUpdated,
            onRenderTracked, onRenderTriggered
          } = Vue;
          const name = ref('dell')
          onBeforeMount(() => {
            console.log('onBeforeMount')
          })
          onMounted(() => {
            console.log('onMounted')
          })
          onBeforeUpdate(() => {
            console.log('onBeforeUpdate')
          })
          onUpdated(() => {
            console.log('onUpdated')
          })
          // 每次渲染后重新收集响应式依赖
          onRenderTracked(() => {
            console.log('onRenderTracked')
          })
          // 每次触发页面重新渲染时自动执行
          onRenderTriggered(() => {
            console.log('onRenderTriggered')
          })
          const handleClick = () => {
            name.value = 'lee'
          }
          return { name, handleClick }
        },
        template: `
          
    {{name}}
    `
    , }); const vm = app.mount('#root');
    script> html>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    Sentinel —实时监控
    linux怎么修改只读权限
    HLS优化设计(一)
    00_socket_demo
    HTML小游戏12 —— 汽车赛道飙车游戏(附完整源码)
    jsp+ssm+maven美容院管理系统--idea mysql
    CS144 计算机网络 Lab3:TCP Sender
    Android 开发用 Kotlin 编程语言 二 条件控制
    springboot+vue+nodejs企业公司财务员工工资管理系统java
    4D5D影院设备发展前景7D互动影院体验馆应用
  • 原文地址:https://blog.csdn.net/weixin_41405524/article/details/126354490