8.0 vue3生命周期

上一篇:vue3 watchEffect函数https://blog.csdn.net/qq_42543244/article/details/122291727

记录vue3的生命周期,于vue2相比,变化并不算很大,最大的变化是,组合式的开发,意味着如果需要在某个生命周期中写点代码,那么前提是必须引入,然后叫法单词上发生的一些变化,先看官网给出的周期图:

vue2:

vue3: 

 然后有两种写法,为了能更好的呈现出来,我就直接贴代码了:

父组件(因为生命周期中有卸载前,已卸载的周期函数,所以我在父组件中进行了v-if的切换)

<template>
  <button @click="isShow = !isShow">点击切换隐藏/显示</button>
  <demo v-if="isShow"></demo>
</template>

<script>
import {
  ref,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
} from "vue";
import Demo from "@/components/Demo.vue";
export default {
  name: "App",
  components: {
    Demo,
  },
  setup() {
    let isShow = ref(true);
    console.log("----parent-setup----");
    onBeforeMount(() => {
      console.log("----parent-onBeforeMount----");
    });
    onMounted(() => {
      console.log("----parent-onMounted----");
    });
    onBeforeUpdate(() => {
      console.log("----parent-onBeforeUpdate----");
    });
    onUpdated(() => {
      console.log("----parent-onUpdated----");
    });
    onBeforeUnmount(() => {
      console.log("----parent-onBeforeUnmount----");
    });
    onUnmounted(() => {
      console.log("----parent-onUnmounted----");
    });
    return { isShow };
  },
};
</script>

子组件(Demo.vue)

<template>
  <div>Demo组件内容</div>
  <div>{{ num }}</div>
  <button @click="num++">点击+1</button>
</template>

<script>
import {
  ref,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
} from "vue";
export default {
  name: "Demo",
  /* 
  vue3 的生命周期和 vue2的很相似,基本保持一致,最后的 destory 改成了 unmount
  使用方法有两种,一种是声明式的,即写在setup外面 与setup同级
  一种是组合式的,需要先进行引入 ,,根据个人喜好选择一种即可,也可以全写,但需要注意执行顺序
  */
  /* beforeCreate() {
    console.log("----beforeCreate----");
  },
  created() {
    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----");
  }, */

  setup() {
    /* 在setup中使用组合式api,生命周期函数需要事先引入,组合式内的生命周期执行顺序是要快于声明式的生命周期
      组合式的引入是没有 onBeforeCreate,onCreated的,认为setup即为beforeCreate,created
    */
    let num = ref(1);
    console.log("----children-setup----");
    onBeforeMount(() => {
      console.log("----children-onBeforeMount----");
    });
    onMounted(() => {
      console.log("----children-onMounted----");
    });
    onBeforeUpdate(() => {
      console.log("----children-onBeforeUpdate----");
    });
    onUpdated(() => {
      console.log("----children-onUpdated----");
    });
    onBeforeUnmount(() => {
      console.log("----children-onBeforeUnmount----");
    });
    onUnmounted(() => {
      console.log("----children-onUnmounted----");
    });

    return { num };
  },
};
</script>

 大概就是这些,演示下,父子组件的渲染顺序

(父组件setup --- > 父组件挂载前 --->  子组件setup--->子组件挂载前--->子组件挂载后--->父组件键挂载后)

 就到这里吧!

下一篇:

vue3 自定义hook函数icon-default.png?t=M0H8https://blog.csdn.net/qq_42543244/article/details/122644055

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay丶萧邦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值