Demo12.vue 890 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <template>
  2. <div>
  3. <h1>Vue3生命周期</h1>
  4. <p id="msg1">{{ msg }}</p>
  5. <!-- <button @click="changeMsg">修改消息</button> -->
  6. </div>
  7. </template>
  8. <!--
  9. vue3生命周期
  10. 初始:setup
  11. 运行:onBeforeMount onMounted onBeforeUpdate onUpdated
  12. 卸载:onBeforeUnmount onUnmounted
  13. -->
  14. <script lang="ts" setup>
  15. import {ref,reactive,onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount,onUnmounted} from "vue"
  16. let msg = ref("121");
  17. console.log(msg.value);
  18. onBeforeMount(()=>{
  19. console.log(msg.value,'创建前')
  20. })
  21. onMounted(()=>{
  22. console.log(msg.value,'创建后')
  23. })
  24. onBeforeUpdate(()=>{
  25. console.log(msg.value,'更新前')
  26. })
  27. onUpdated(()=>{
  28. console.log(msg.value,'更新后')
  29. })
  30. onBeforeUnmount(()=>{
  31. console.log('卸载前')
  32. })
  33. onUnmounted(()=>{
  34. console.log('卸载后')
  35. })
  36. </script>
  37. <style lang="scss" scoped>
  38. </style>