zoukankan      html  css  js  c++  java
  • vue生命周期详解

    gitHub地址:https://github.com/huangpna/vue_learn/example里面的lesson12

    首先,每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期。首先看一张图吧~这是官方文档上的图片相信大家一定都会很熟悉:

    可以看到在vue一整个的生命周期中会有很多钩子函数提供给我们在vue生命周期不同的时刻进行操作, 那么先列出所有的钩子函数,然后我们再一一详解:

    •  beforeCreate   组件实例刚刚被创建,属性都没有
    • created    实例已经被创建完成,属性绑定
    • beforeMount    模板编译之前
    • mounted    模板编译之后
    • beforeUpdate  组件更新之前
    • updated      组件更新完毕
    • beforeDestroy 组件销毁前
    • destroyed     组件销毁后

    先来一波代码,各位复制在浏览器中运行,打开console查看就行了:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>vue生命周期学习</title>
    </head>
    <body>
        <div id="app1">
            <h1>{{message}}</h1>
        </div>
    </body>
    <script src="../js/vue.js"></script>
    <script>
        /**
         *  生命周期
         *
         * */
        var vm = new Vue({
            el:'#app1',
            data:{
                message: 'Vue的生命周期'
            },
            beforeCreate: function() {
                console.group('------beforeCreate创建前状态------');
                console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
                console.log("%c%s", "color:red","data   : " + this.$data); //undefined
                console.log("%c%s", "color:red","message: " + this.message)
            },
            created: function() {
                console.group('------created创建完毕状态------');
                console.log("%c%s", "color:red","el     : " + this.$el); //undefined
                console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red","message: " + this.message); //已被初始化
            },
            beforeMount: function() {
                console.group('------beforeMount挂载前状态------');
                console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red","message: " + this.message); //已被初始化
            },
            mounted: function() {
                console.group('------mounted 挂载结束状态------');
                console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red","message: " + this.message); //已被初始化
            },
            beforeUpdate: function () {
                console.group('beforeUpdate 更新前状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message);
            },
            updated: function () {
                console.group('updated 更新完成状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message);
            },
            beforeDestroy: function () {
                console.group('beforeDestroy 销毁前状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message);
            },
            destroyed: function () {
                console.group('destroyed 销毁完成状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message)
            }
        });
    </script>
    </html>

    运行后打开console可以看到打印出来内容如下:

    可以看到一个vue实例在创建过程中调用的几个生命周期钩子。

    1、在beforeCreate和created钩子函数之间的生命周期

    在这个生命周期之间,进行初始化事件,进行数据的观测,可以看到在created的时候数据已经和data属性进行绑定(放在data中的属性当值发生改变的同时,视图也会改变)。
    注意看下:此时还是没有el选项

    2、created钩子函数和beforeMount间的生命周期

    在这一阶段发生的事情还是比较多的。

    首先会判断对象是否有el选项如果有的话就继续向下编译,如果没有el选项,则停止编译,也就意味着停止了生命周期,直到在该vue实例上调用vm.$mount(el)。此时注释掉代码中:

    el: '#app1',

    然后运行可以看到到created的时候就停止了:

    如果我们在后面继续调用vm.$mount(el),可以发现代码继续向下执行了

    vm.$mount(el) //这个el参数就是挂在的dom节点

    然后,我们往下看,template参数选项的有无对生命周期的影响。
    (1).如果vue实例对象中有template参数选项,则将其作为模板编译成render函数。
    (2).如果没有template选项,则将外部HTML作为模板编译。
    (3).可以看到template中的模板优先级要高于outer HTML的优先级。
    修改代码如下, 在HTML结构中增加了一串html,在vue对象中增加了template选项

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>vue生命周期学习</title>
    </head>
    <body>
        <div id="app1">
            <h1>{{message}}</h1>
        </div>
    </body>
    <script src="../js/vue.js"></script>
    <script>
        /**
         *  生命周期
         *
         * */
        var vm = new Vue({
            el:'#app1',
            template:'<h1>{{message}}-----这是template模板中的</h1>',
            data:{
                message: 'Vue的生命周期'
            },
            beforeCreate: function() {
                console.group('------beforeCreate创建前状态------');
                console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
                console.log("%c%s", "color:red","data   : " + this.$data); //undefined
                console.log("%c%s", "color:red","message: " + this.message)
            },
            created: function() {
                console.group('------created创建完毕状态------');
                console.log("%c%s", "color:red","el     : " + this.$el); //undefined
                console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red","message: " + this.message); //已被初始化
            },
            beforeMount: function() {
                console.group('------beforeMount挂载前状态------');
                console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red","message: " + this.message); //已被初始化
            },
            mounted: function() {
                console.group('------mounted 挂载结束状态------');
                console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red","message: " + this.message); //已被初始化
            },
            beforeUpdate: function () {
                console.group('beforeUpdate 更新前状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message);
            },
            updated: function () {
                console.group('updated 更新完成状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message);
            },
            beforeDestroy: function () {
                console.group('beforeDestroy 销毁前状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message);
            },
            destroyed: function () {
                console.group('destroyed 销毁完成状态===============》');
                console.log("%c%s", "color:red","el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red","data   : " + this.$data);
                console.log("%c%s", "color:red","message: " + this.message)
            }
        });
    </script>
    </html>

    执行后的结果可以看到在页面中显示的是:

    那么将vue对象中template的选项注释掉后打印如下信息:

    这下就可以想想什么el的判断要在template之前了~是因为vue需要通过el找到对应的template。

    在vue对象中还有一个render函数,它是以createElement作为参数,然后做渲染操作,而且我们可以直接嵌入JSX.

    new Vue({
        el: '#app1',
        render: function(createElement) {
            return createElement('h1', 'this is createElement')
        }
    })

    可以看到页面中渲染的是:

    所以综合排名优先级:
    render函数选项 > template选项 > outer HTML(body中HTML).

    3、beforeMount和mounted钩子函数间的生命周期

     

    可以看到此时是给vue实例对象添加$el成员,并且替换掉挂在的DOM元素。因为在之前console中打印的结果可以看到beforeMount之前el上还是undefined。

    4、mounted

    注意看下面截图:

    在mounted之前h1中还是通过{{message}}进行占位的,因为此时还有挂在到页面上,还是JavaScript中的虚拟DOM形式存在的。在mounted之后可以看到h1中的内容发生了变化。

    5、beforeUpdate钩子函数和updated钩子函数间的生命周期

    当vue发现data中的数据发生了改变,会触发对应组件的重新渲染,先后调用beforeUpdateupdated钩子函数。我们在console中输入:

    vm.message = '触发组件更新'

    发现触发了组件的更新:

    6、beforeDestroy和destroyed钩子函数间的生命周期

     

    beforeDestroy钩子函数在实例销毁之前调用。在这一步,实例仍然完全可用。
    destroyed钩子函数在Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。

    本文是个人对vue的生命周期的理解,有什么不对的地方还请大牛多多指点~~

  • 相关阅读:
    使用Spring的ReloadableResourceBundleMessageSource读取properties配置
    IFrame自适应高度
    Js返回页面顶部
    复制页面内容时添加额外信息
    怎样将Excel中的日期格式转换为文本格式
    为Tomcat页面设置访问权限(HTTP)
    为tomcat页面设置访问权限(BASIC认证)
    离线安装Maven FOR Eclipse插件
    sharepoint 获得上级和部门的封装函数
    SharePoint定制开发个性皮肤
  • 原文地址:https://www.cnblogs.com/1156063074hp/p/10174675.html
Copyright © 2011-2022 走看看