VUE生命周期和钩子函数的理解

154 阅读2分钟

VUE生命周期和钩子函数 每个vue实例在被创建前都会经过一系列的初始化过程,Vue实例有一个完整的生命周期,也就是从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、卸载等一系列过程,这个过程就叫做vue的生命周期。 在很多时候,我们在vue的生命周期中需要执行一些操作。所以就产生了生命周期钩子函数。

L69ML~@W5AIQ@VAVPA%RG90.png 这是来自vue官方文档的整个流程。其中钩子函数有以下这些: 钩子函数 beforeCreate/ created/ beforeMount/ mounted/ beforeUpdate/ update/ beforeDestroy/ destroyed/ 具体代码如下: `

Vue生命周期

{{message}}

<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            message: 'Vue生命周期'
        },
        beforeCreate: function () {
            console.group('-----beforeCreate-----');
            console.log("%c%s", "color:red", "el      :" + this.$el);
            console.log("%c%s", "color:red", "data    :" + this.$data);
            console.log("%c%s", "color:red", "message :" + this.message);
        },
        created: function () {
            console.group('-----created-----');
            console.log("%c%s", "color:red", "el:     :" + this.$el);
            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>
` beforeCreate 在实例初始化之后,数据观测(data observer) 和 event/watcher 事件配置之前被调用。

created 实例已经创建完成之后被调用。在这一步,实例已完成以下的配置:数据观测(data observer),属性和方法的运算, watch/event 事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。

beforeMount 在挂载开始之前被调用:相关的 render 函数首次被调用。

mounted el 被新创建的 vm.el 替换,并挂载到实例上去之后调用该钩子。如果 root 实例挂载了一个文档内元素,当 mounted 被调用时 vm.$el 也在文档内。

beforeUpdate 数据更新时调用,发生在虚拟 DOM 重新渲染和打补丁之前。 你可以在这个钩子中进一步地更改状态,这不会触发附加的重渲染过程。

updated 由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。

当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态,因为这可能会导致更新无限循环。

该钩子在服务器端渲染期间不被调用。

beforeDestroy 实例销毁之前调用。在这一步,实例仍然完全可用。

destroyed Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。 该钩子在服务器端渲染期间不被调用。