actived生命周期_Vuejs 生命周期

本文详细介绍了Vue实例的生命周期,包括beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed等关键阶段。还提到了与Vue缓存组件相关的activated和deactivated生命周期钩子,以及它们在页面性能优化和全局事件管理中的应用。
摘要由CSDN通过智能技术生成

每个 Vue 实例在被创建时都要经过一系列的初始化过程。如需要设置数据监听、编译模板、将实例挂载到 DOM 并在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数,这给了用户在不同阶段添加自己的代码的机会。

一个 Vue 实例

我们创建一个 Vue 实例,并在每个阶段验证 Vue 的生命周期函数。Vue 实例的生命周期函数并不放置在 methods里,而是单独放置在 Vue 的实例之中。html>

Vue实例生命周期函数

//生命周期函数就是Vue实例在某一个时间点会自动执行的函数

var vm = new Vue({      el: "#app",      template: "

{{test}}
",      data: {        test: "hello world"

},      // Vue初始化

beforeCreate: function(){        console.log('beforeCreate')

},      created: function(){        console.log('created')

},      //页面渲染之前 挂载之前的时间点

beforeMount: function(){        console.log('beforeMount')        console.log(this.$el)

},      // 挂载到页面之后

mounted: function(){        console.log('mounted')        console.log(this.$el)

},      //当数据发生改变的时候 执行

beforeUpdate: function(){        console.log('beforeUpdate')

},      updated: function(){        console.log('updated')

},      // vm.$destroy()被called的时候触发

beforeDestroy: function(){        console.log('beforeDestroy')

},      destroyed: function(){        console.log('destroyed')

}

})  

生命周期函数

生命周期函数就是Vue实例在某一个时间点会自动执行的函数Vue 初始化:beforeCreate 、created

页面渲染之前:beforeMount

挂载到页面之后:mounted

当数据发生改变时:beforeDestroy(重新渲染之前) 、destroyed(重新渲染之后)

vm.$destroy()被调用时,即组件被销毁时:beforeDestroy 、destroyed

生命周期图

下图展示了 Vue 实例的声明周期,结合上一节的示例,可以更好的理解 Vue 的这八个声明周期钩子。

其他

除此之外 Vue 还拥有 activated、deactivated 、errorCaptured 这三个生命周期钩子。

activated

当页面重新显示的时候,执行。搭配 keep-alive、localStrage 和临时变量做页面性能优化。

deactivated

当页面即将被隐藏或替换成其他页面时,执行。可以用来解绑在 activated 上绑定的全局事件。

作者:evenyao

链接:https://www.jianshu.com/p/c8a7306aa844

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值