vue生命周期(举例)

2020版
根据视频整理,链接: 视频链接
P31—生命周期1
生命周期图
在这里插入图片描述
一般请求网络数据等操作要么在created(组件初始化之前)要么在mounted(组件初始化之后)

P32—生命周期2
在方框对应位置是初始化数据和方法
在这里插入图片描述
所以对应的执行顺序如下:

export default {
  name: 'LifeCircle',
  beforeCreate () {
    console.log('1.beforeCreate')
  },

  data () {
    return {
      isShow: false,
      str1: 'str-----',
      str2: 'str====='
    }
  },
  methods: {},
  created () {
    console.log('2.created')
  },
  beforeMount () {
    console.log('3.beforeMount')
  },
  mounted () {
    console.log('4.mounted')
    // 定时器
    setInterval(() => {
      console.log('+++++++++++++++++++++')
      this.isShow = !this.isShow
    }, 1000)
  },
  beforeUpdate () {
    console.log('5.beforeUpdate')
  },
  updated () {
    console.log('6.updated')
  },
  beforeDestroy () {
    console.log('7.beforeDestroy')
  },
  destroyed () {
    console.log('8.destroyed')
  }
}

写个小demo看看执行了哪些

<template>
  <div>
    <p v-if='isShow'>{{str1}}</p>  <!--这里注意isShow一定要加引号-->
    <p v-else>{{str2}}</p>
  </div>
</template>

执行打印出:
在这里插入图片描述
下面测试销毁:注册一个事件

methods: {
    destroy () {
      // 销毁一个实例或者当前组件,直接用this
      this.$destroy()
    }
  }

点击之后,所有周期走完,但是定时器没有被清掉
在这里插入图片描述
销毁时要格外注意清除定时器

  1. 给定时器加id,为了拿到,要加this.
this.interId = setInterval(() => {
      console.log('+++++++++++++++++++++')
      this.isShow = !this.isShow
    }, 1000)

2.在beforeDestroy里面把定时器的id给clearxxx

beforeDestroy () {
    console.log('7.beforeDestroy')
    // 清除定时器
    clearInterval(this.interId)
  }

此时:
在这里插入图片描述

汇总以上代码:
1-在LifeCircle.vue中的代码:

<template>
  <div>
    <p v-if='isShow'>{{str1}}</p>
    <p v-else>{{str2}}</p>
    <button @click='destroy'>销毁</button>
  </div>
</template>

<script>
export default {
  name: 'LifeCircle',
  beforeCreate () {
    console.log('1.beforeCreate')
  },

  data () {
    return {
      isShow: false,
      str1: 'str-----',
      str2: 'str====='
    }
  },
  methods: {
    destroy () {
      // 销毁一个实例或者当前组件,直接用this
      this.$destroy()
    }
  },
  created () {
    console.log('2.created')
  },
  beforeMount () {
    console.log('3.beforeMount')
  },
  mounted () {
    console.log('4.mounted')
    // 定时器
    this.interId = setInterval(() => {
      console.log('+++++++++++++++++++++')
      this.isShow = !this.isShow
    }, 1000)
  },
  beforeUpdate () {
    console.log('5.beforeUpdate')
  },
  updated () {
    console.log('6.updated')
  },
  beforeDestroy () {
    console.log('7.beforeDestroy')
    // 清除定时器
    clearInterval(this.interId)
  },
  destroyed () {
    console.log('8.destroyed')
  }
}
</script>

<style scoped>

</style>

2-在App.vue中的代码(注意第三行)

<template>
  <div id="app">
    <LifeCircle/>
  </div>
</template>

<script>
import LifeCircle from './components/LifeCircle'

export default {
  name: 'App',
  components: {
    LifeCircle
  }
}
</script>

<style>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值