VUE之生命周期

金歌  金牌会员 | 2025-1-16 11:11:55 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 923|帖子 923|积分 2769

目录
1、VUE2的生命周期
1.1、创建(创建前,创建完毕)
1.2、挂载(挂载前,挂载完毕)
1.3、更新(更新前,更新完毕)
1.4、烧毁(烧毁前,烧毁完毕)
2、VUE3的生命周期
2.1、创建(setup)
2.2、挂载(onBeforeMount、onMounted)
2.3、更新(onBeforeUpdate、onUpdated)
2.4、卸载(onBeforeUnmount、OnUnmounted)
3、父和子的生命周期


生命周期整体分为四个阶段,分别是:创建、挂载、更新、烧毁,每个阶段都有两个钩子,一前一后。
1、VUE2的生命周期



  • 创建阶段:beforeCreate、created
  • 挂载阶段:beforeMount、mounted
  • 更新阶段:beforeUpdate、updated
  • 烧毁阶段:beforeDestory、destroyed
1.1、创建(创建前,创建完毕)

  1. <template>
  2. <Person/>
  3. </template>
  4. <script>
  5. import Person from './components/Person.vue'
  6. export default {
  7.   name: 'App',
  8.   components:{Person}
  9. }
  10. </script>
复制代码
  1. <!-- eslint-disable vue/multi-word-component-names -->
  2. <template>
  3. <div class="person">
  4. <h2>当前求和为:{
  5.   { sum }}</h2>
  6. <button @click="add">点我sum+1</button>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11.   // eslint-disable-next-line vue/multi-word-component-names
  12.   name: 'person',
  13.   data(){
  14.     return {
  15.       sum: 1
  16.     }
  17.   },
  18.   methods:{
  19.      add(){
  20.       this.sum += 1
  21.      }
  22.   },
  23.   //创建前的钩子
  24.   beforeCreate(){
  25.     console.log('创建前')
  26.   },
  27.   //创建完毕的钩子
  28.   created(){
  29.     console.log('创建完毕')
  30.   }
  31. }
  32. </script>
  33. <style scoped>
  34.   .person {
  35.     background-color: skyblue;
  36.     padding: 20px;
  37.     border-radius: 10px;
  38.     box-shadow: 0 0 10px;
  39.   }
  40. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表