浏览器页面与vue2.0组件生命周期横向比较

chromevue2.0description
----beforeCreate组件实例刚被创建,组件属性计算之前,如data属性
----created组件实例创建完成,属性已绑定,但DOM还为生成,$el还不存在
document.readyState === loading----页面正在加载中
document.readyState === interactive----页面解析完毕
DOMContentloaded----DOM构建完毕时触发
----beforeMounted模板编译/挂载之前
----mounted模板编译/挂载之后
document.readyState === complete----文档和所有子资源已完成加载。状态表示 load 事件即将被触发
load事件触发----页面所有资源都加载完毕之后
----beforeUpdate组件更新之前
----update组件更新之后
----activatedfor keep-alive,组件被激活时调用
----deactivatedfor keep-alive,组件被移除时调用
beforeunload事件触发beforeDestory组件销毁前调用
unload事件触发Destoryed组件销毁后调用

浏览器页面加载生命周期测试用例:

<!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>
    <script>
        function log (text) {console.log(text)};
        var startDate = new Date();
        log(Date.now() - startDate + 'initial state' + document.readyState);
        document.addEventListener('DOMContentLoaded', () => log(Date.now() - startDate + 'DOMContentLoaded'));
        document.addEventListener('readystatechange', () => log('document.readystate:', document.readyState));

        window.addEventListtener('onload', () => log('window onloaded'));
    <iframe onload='log("iframe loaded")'></iframe>
    <img src='' alt='blank'><img>
    
</body>

</html>
复制代码

vue2.0组件生命周期测试用例:

<!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>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>{{ message }}</p>
    </div>
    <script type='text/javascript'>
        var app = new Vue({
            el: '#app',
            data() {
                return { message: 'vue life cycle' }
            },
            methods: {
                log: function (text) {
                    console.log("%c%s", "color:red", text)
                }
            },
            beforeCreate: function () {
                console.group('beforeCreate 创建前状态===============》');
                console.log('el: ' + this.$el);
                console.log(this.$el);
                console.log('data: ' + this.$data);
                console.log('message: ' + this.message);
                console.log('methods: ' + this.$methods);
            },
            created: function () {
                console.group('created 创建后状态===============》');
                console.log('el: ' + this.$el);
                console.log(this.$el);
                console.log('data: ' + this.$data);
                console.log('message: ' + this.message);
                console.log('methods: ' + this.$methods);
            },
            beforeMount: function () {
                console.group('beforeMount 模板加载前状态===============》');
                console.log('el: ' + this.$el);
                console.log(this.$el);
                console.log('data: ' + this.$data);
                console.log('message: ' + this.message);
                console.log('methods: ' + this.$methods);
            },
            mounted() {
                console.group('mounted 模板加载后状态===============》');
                console.log('el: ' + this.$el);
                console.log(this.$el);
                console.log('data: ' + this.$data);
                console.log('message: ' + this.message);
                console.log('methods: ' + this.$methods);
            },
            beforeDestroy() {
                console.group('beforeDestory 销毁前状态===============》');
                console.log('el: ' + this.$el);
                console.log(this.$el);
                console.log('data: ' + this.$data);
                console.log('message: ' + this.message);
                console.log('methods: ' + this.$methods);
            },
            destroyed() {
                console.group('destoryed 创建前状态===============》');
                console.log('el: ' + this.$el);
                console.log(this.$el);
                console.log('data: ' + this.$data);
                console.log('message: ' + this.message);
                console.log('methods: ' + this.$methods);
            }

        })
    </script>
</body>

</html>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值