Vue 生命周期和钩子函数

Vue 生命周期和钩子函数

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <p>{{ message }}</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                message: "1"
            },
            beforeCreate: function () {
                console.log('beforeCreate');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            created: function () {
                console.log('created');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            beforeMount: function () {
                console.log('beforeMount');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            mounted: function () {
                console.log('mounted');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            beforeUpdate: function () {
                console.log('beforeUpdate');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            updated: function () {
                console.log('updated');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            beforeDestroy: function () {
                console.log('beforeDestroy');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            },
            destroyed: function () {
                console.log('destroyed');
                console.log("DOM = " + (this.$el && this.$el.innerText));
                console.log("$data.message = " + (this.$data && this.$data.message));
                console.log('-----------------------------------------------');
            }
        })
    </script>
</body>

</html>

这里写图片描述

  • beforeCreate : 组件实例刚被创建。属性和 DOM 都没有被初始化。
  • created : 组件实例创建完成。属性绑定成功、DOM 未初始化。
  • beforeMount : 模板挂载之前。属性绑定成功、DOM 未渲染。
  • mounted : 模板挂载完成。属性绑定成功、DOM 渲染成功。
  • beforeUpdate : 组件更新之前。属性更新成功、DOM 未更新
  • updated : 组件更新完成。属性和 DOM 更新成功
  • beforeDestory : 组件销毁前调用。属性和 DOM 依然存在。
  • destoryed : 组件已销毁。属性和 DOM 依然存在。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值