• vue3快速入门-生命周期


    此系列文章合集click me

    此系列文章源码click me


    对比表

    Vue2对比Vue3执行阶段
    beforeCreate==>setup()
    created==>setup()组件创建之前
    beforeMount==>onBeforeMount组件挂载到节点之前
    mounted==>onMounted组件挂载完成后
    beforeUpdate==>onBeforeUpdate组件更新之前
    updated==>onUpdated组件更新完成之后
    beforeDestroy==>onBeforeUnmount组件卸载之前
    destroyed==>onUnmounted组件卸载完成
    activated==>onActivated进入缓存组件时
    deactivated==>onDeactivated离开缓存组件时
    errorCaptured==>onErrorCaptured当子孙组件出错时
    新增onRenderTriggered跟踪虚拟 DOM 重新渲染时调用
    新增onRenderTracked当虚拟 DOM 重新渲染被触发时调用

    lifeCycle.vue

    <template>
      <div id="app">
        <h2>msg: {{ msg }}</h2>
        <button @click="updateMsg">更新msg</button>
    
        <button @click="goKeepAliveCom">前往缓存组件</button>
      </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    <script lang="ts">
    import {
      ref,
      defineComponent,
      onBeforeMount,
      onBeforeUnmount,
      onBeforeUpdate,
      onMounted,
      onUnmounted,
      onUpdated,
    } from "vue";
    import { useRouter } from "vue-router";
    
    export default defineComponent({
      setup() {
        const msg = ref("温情");
    
        const updateMsg = () => {
          msg.value += "K";
        };
    
        const router = useRouter();
    
        const goKeepAliveCom = () => {
            router.push('/keepAlive');
        }
    
        onBeforeMount(() => {
          console.log("onBeforeMount-组件挂载到节点之前");
        });
    
        onMounted(() => {
          console.log("onMounted-组件挂载完成后");
        });
    
        onBeforeUpdate(() => {
          console.log("onBeforeUpdate-组件更新之前");
        });
    
        onUpdated(() => {
          console.log("onUpdated-组件更新完成之后");
        });
    
        onBeforeUnmount(() => {
          console.log("onBeforeUnmount-组件卸载之前");
        });
    
        onUnmounted(() => {
          console.log("onUnmounted-组件卸载完成");
        });
    
        console.log("setup-开始创建组件之前");
    
        return {
          msg,
          updateMsg,
          goKeepAliveCom
        };
      },
    });
    </script>
    
    • 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

    keep-alive.vue

    <template>
      <div id="app">
        <input type="text" v-model="ipt" />
        <br />
        <button @click="router.back()">返回</button>
      </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    <script lang="ts">
    import { defineComponent, onActivated, onDeactivated, onMounted, ref } from "vue";
    import { useRouter } from "vue-router";
    
    export default defineComponent({
      setup() {
        const ipt = ref("");
    
        const router = useRouter();
    
        onMounted(() => {
            console.warn("只有第一次进入缓存组件才触发onMounted")
        })
    
        onActivated(() => {
          console.log("进入缓存组件onActivated");
        });
    
        onDeactivated(() => {
          console.log("离开缓存组件onDeactivated");
        });
    
        return { ipt, router };
      },
    });
    </script>
    
    • 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

    由于输出的实在太多,截图不便于理解,所以建议自己动手试试。

  • 相关阅读:
    matlab神经网络训练方法,matlab神经网络训练图
    【信号和槽】
    7_1数据结构与算法基础:::数据结构
    第五章 MyBais插件
    Postman接口测试工具
    Ubuntu安装SVN服务并结合内网穿透实现公网访问本地存储文件
    深度学习模型部署与优化:策略与实践;L40S与A100、H100的对比分析
    Docker入门
    分布式之存储高可用
    语义分割实战:基于tensorflow搭建DeeplabV3实现语义分割任务
  • 原文地址:https://blog.csdn.net/qq_48960335/article/details/125398494