Vue的生命周期

Vue实例有一个完整的生命周期,也就是从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、卸载等一系列过程,我们称这是Vue的生命周期。通俗说就是Vue实例从创建到销毁的过程,就是生命周期。

在组件中具体的方法有:

  beforeCreate、created(此时需说明可以在created中首次拿到data中定义的数据)、beforeMount、mounted(此时需说明dom树渲染结束,可访问dom结构)、beforeUpdate、updated、beforeDestroy、destroyed

下面是Vue生命周期的流程图:

 下面是一个检验lifeCycle的小程序:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../vue.js"></script>
    <title>lifeCycle</title>
</head>
<body>
    <div id="app">
        <h1 :style="{opacity}">你好世界</h1>
        <button @click="onclick()">点我透明度变高</button>
    </div>
    <script>
        let vm=new Vue({
            el:'#app',
            data:{
                opacity: 1
            },
            methods:{
                onclick(){
                    this.opacity-=0.2;
                }
            },
            computed:{
        
            },
            beforeCreate() {
                console.log("beforeCreate,啥都没做")
            },
            created() {
                console.log("created,配置了代理对象vm")
            },
            beforeMount() {
                console.log("beforeMount,有虚拟dom但还没有挂载到真实dom")
            },
            mounted() {
                console.log("mounted,挂载虚拟dom")
            },
            beforeUpdate() {
                console.log("beforeUpdate,更新了数据但还没改变到页面上")
            },
            updated() {
                console.log("updated,数据和页面展示均已更新")
            },
            beforeDestroy() {
                console.log("beforeDestroy,收尾工作,关闭定时器,解绑自定义事件,取消订阅消息等")
            },
            destroyed() {
                console.log("destroyed,销毁data,methods,指令等...")
            },
        })
    </script>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值