移动端项目总结 - Vue 生命周期函数

beforeCreated:不能读到data中的数据

beforeCreate() {
  console.log('beforeCreate()', this.title);
}

从不能读取data中的数据,到可以读取,这中间实现了数据代理,数据响应式

created:自created之后,就可以读到data中的数据

created() {
  console.log('created()', this.tittle);
}

beforeMount:还不能读取组件的$refs属性

beforeMount() {
  console.log('beforeMount()', this.$refs.tittle);
}

mounted表示已经挂载,也就是说已经显示,但是beforeMounted还没有显示。还没有挂载的组件其$refs属性是无法读取的,只有挂载之后才能读取。这期间,还会做初始化显示。

mounted:可以读取组件的$refs属性

mounted() {
  console.log('mounted()', this.$refs.tittle);
}

beforeUpdate:界面更新前。数据已经更新,但是界面还没有更新

beforeUpdate() {
  console.log('beforeUpdate()', this.$refs.title.textCount);  // 通过查看页面中的数据是老的数据还是新数据,就可以看到是在页面更新前,还是在页面更新后。
}

界面更新。

update:页面更新过后

updated() {
  console.log('updated()', this.$refs.title.textCount);
}

beforeDestroy:在此处做一些清理工作

beforyDestory() {
  console.log('beforyDestory()');
}

destroyed:

destoryed() {
  console.log('destoryed()');
}

主要针对路由组件:前提是路由组件用上<keep-alive>,只有用上了keep-alive才有失活激活的概念,也就是加了之后,组件之间进行切换,切换到的组件就可以激活,被切换走的组件就会失活。Component组件指定一个is也可以动态加载组件,但是这个一般用得比较少。

beforeActivat:失活,表示不再显示了,并被一个组件死亡

beforeActive() {
  console.log('beforeActive()');
}

activated:激活

actived() {
  console.log('actived()');
}

可以用在监测路由组件之间的切换,父组件一定要用keep-alive将其包裹。 

<!-- 父组件 -->
<template>
  <div>
    <router-link to="/review/mixin">mixin</router-link>
    <router-link to="/review/life">life</router-link>
    <hr>
    <keep-alive>
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

麦田里的POLO桔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值