当前位置:   article > 正文

vue3 生命周期 读取dom元素 / 父组件与子组件的生命周期表变化_vue3 获取父组件的dom

vue3 获取父组件的dom

 

  1. <template>
  2. <p> 我是App.vue组件 </p>
  3. <HelloWorld msg="Hello Vue 3 + Vite"/>
  4. </template>
  5. <script setup lang="ts">
  6. import HelloWorld from "@/components/HelloWorld.vue";
  7. import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated} from "vue";
  8. //beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed errorCaptured
  9. //beforeCreate created 如果是setup语法糖模式是没有这两个生命周期的,setup去代替
  10. console.log("setup")
  11. //创建
  12. onBeforeMount(() => {
  13. console.log("onBeforeMount")
  14. })
  15. //创建完成
  16. onMounted(() => {
  17. console.log("onMounted")
  18. })
  19. //更新组件之前
  20. onBeforeUpdate(() => {
  21. console.log("onBeforeUpdate")
  22. })
  23. //更新完成
  24. onUpdated(() => {
  25. console.log("onUpdated")
  26. })
  27. //销毁之前
  28. onBeforeUnmount(() => {
  29. console.log("onBeforeUnmount")
  30. })
  31. //销毁完成
  32. onUnmounted(() => {
  33. console.log("onUnmounted")
  34. })
  35. </script>

 先走setup 然后创建之前,创建完成。

创建之前是无法读取dom元素的

  1. <template>
  2. <p> 我是App.vue组件 </p>
  3. <div ref="div">{{ div }}</div>
  4. </template>
  5. <script setup lang="ts">
  6. import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref} from "vue";
  7. //beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed errorCaptured
  8. //beforeCreate created 如果是setup语法糖模式是没有这两个生命周期的,setup去代替
  9. console.log("setup")
  10. const str = ref<String>("123")
  11. const div = ref<HTMLDivElement>()
  12. //创建
  13. onBeforeMount(() => {
  14. console.log("onBeforeMount",div.value)
  15. })
  16. //创建完成
  17. onMounted(() => {
  18. console.log("onMounted",div.value)
  19. })
  20. //更新组件之前
  21. onBeforeUpdate(() => {
  22. console.log("onBeforeUpdate")
  23. })
  24. //更新完成
  25. onUpdated(() => {
  26. console.log("onUpdated")
  27. })
  28. //销毁之前
  29. onBeforeUnmount(() => {
  30. console.log("onBeforeUnmount")
  31. })
  32. //销毁完成
  33. onUnmounted(() => {
  34. console.log("onUnmounted")
  35. })
  36. </script>

读取dom元素方法很简单

  <div ref="div">{{ div }}</div>

const div = ref<HTMLDivElement>()

如何走到update生命周期

  1. <template>
  2. <p> 我是App.vue组件 </p>
  3. <div ref="div">{{ str }}</div>
  4. <button @click="change">修改str</button>
  5. </template>
  6. <script setup lang="ts">
  7. import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref} from "vue";
  8. //beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed errorCaptured
  9. //beforeCreate created 如果是setup语法糖模式是没有这两个生命周期的,setup去代替
  10. console.log("setup")
  11. const str = ref<String>("123")
  12. const div = ref<HTMLDivElement>()
  13. //创建
  14. onBeforeMount(() => {
  15. console.log("onBeforeMount", div.value)
  16. })
  17. //创建完成
  18. onMounted(() => {
  19. console.log("onMounted", div.value)
  20. })
  21. //更新组件之前
  22. onBeforeUpdate(() => {
  23. console.log("onBeforeUpdate")
  24. })
  25. //更新完成
  26. onUpdated(() => {
  27. console.log("onUpdated")
  28. })
  29. //销毁之前
  30. onBeforeUnmount(() => {
  31. console.log("onBeforeUnmount")
  32. })
  33. //销毁完成
  34. onUnmounted(() => {
  35. console.log("onUnmounted")
  36. })
  37. const change = () => {
  38. str.value = "456"
  39. }
  40. </script>

 使用change 函数修改str即可 

  1. //更新组件之前
  2. onBeforeUpdate(() => {
  3. console.log("onBeforeUpdate",div.value?.innerText)
  4. })
  5. //更新完成
  6. onUpdated(() => {
  7. console.log("onUpdated",div.value?.innerText)
  8. })

 销毁之前,与,销毁。

修改App.vue代码

  1. <template>
  2. <p> 我是App.vue组件 </p>
  3. <A v-if="flag"></A>
  4. <button @click="flag = !flag">创建-销毁</button>
  5. </template>
  6. <script setup lang="ts">
  7. import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref} from "vue";
  8. import A from "./components/A.vue";
  9. //beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed errorCaptured
  10. //beforeCreate created 如果是setup语法糖模式是没有这两个生命周期的,setup去代替
  11. console.log("setup")
  12. const flag = ref<Boolean>(false)
  13. const str = ref<String>("123")
  14. const div = ref<HTMLDivElement>()
  15. //创建
  16. onBeforeMount(() => {
  17. console.log("onBeforeMount", div.value)
  18. })
  19. //创建完成
  20. onMounted(() => {
  21. console.log("onMounted", div.value)
  22. })
  23. //更新组件之前
  24. onBeforeUpdate(() => {
  25. console.log("onBeforeUpdate", div.value?.innerText)
  26. })
  27. //更新完成
  28. onUpdated(() => {
  29. console.log("onUpdated", div.value?.innerText)
  30. })
  31. //销毁之前
  32. onBeforeUnmount(() => {
  33. console.log("onBeforeUnmount")
  34. })
  35. //销毁完成
  36. onUnmounted(() => {
  37. console.log("onUnmounted")
  38. })
  39. const change = () => {
  40. str.value = "456"
  41. }
  42. </script>

修改A.vue代码

  1. <template>
  2. <p> 我是A组件 </p>
  3. </template>
  4. <script setup lang="ts">
  5. import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref} from "vue";
  6. //beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed errorCaptured
  7. //beforeCreate created 如果是setup语法糖模式是没有这两个生命周期的,setup去代替
  8. console.log("setup")
  9. const str = ref<String>("123")
  10. const div = ref<HTMLDivElement>()
  11. //创建
  12. onBeforeMount(() => {
  13. console.log("A onBeforeMount", div.value)
  14. })
  15. //创建完成
  16. onMounted(() => {
  17. console.log("A onMounted", div.value)
  18. })
  19. //更新组件之前
  20. onBeforeUpdate(() => {
  21. console.log("A onBeforeUpdate", div.value?.innerText)
  22. })
  23. //更新完成
  24. onUpdated(() => {
  25. console.log("A onUpdated", div.value?.innerText)
  26. })
  27. //销毁之前
  28. onBeforeUnmount(() => {
  29. console.log("A onBeforeUnmount")
  30. })
  31. //销毁完成
  32. onUnmounted(() => {
  33. console.log("A onUnmounted")
  34. })
  35. const change = () => {
  36. str.value = "456"
  37. }
  38. </script>

首次加载

 点击创建

 点击销毁

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/133908
推荐阅读
相关标签
  

闽ICP备14008679号