美文网首页
Vue 基础【3】— 生命周期

Vue 基础【3】— 生命周期

作者: 弱冠而不立 | 来源:发表于2020-11-23 13:40 被阅读0次

生命周期示例图:

各个生命周期的作用

生命周期 描述
beforeCreate 组件实例被创建之前,组件的属性生效之前
created 组件实例已经被创建,属性也绑定了,但是真实DOM未生成,$el不可用
beforeMount 开始挂载之前被调用,首次调用render函数
mounted 实例被挂载到DOM上之后被调用
beforeUpdate data中数据是新的,但是页面上的数据还没更新
update 组件数据更新之后,页面和data数据已经同步
activated keep-alive专属,组件被激活时调用
deactivated keep-alive专属,组件被销毁时调用
beforeDestory 组件被销毁前调用
destoryed 组件被销毁时调用

对于生命周期的规律,可以自己写个小样例来探究一下

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lifeCycle-demo</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <div>{{ message }}</div>
        <input type="text" v-model="message">
       <button @click="destroyApp">destroyApp</button>
    </div>
    
</body>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            message: 'Hello Vue!'
        },
        methods: {
            destroyApp: function() {
                //清理它与其它实例的连接,解绑它的全部指令及事件监听器。
                console.log(this.$destroy());
            }
        },
        beforeCreate() {
            console.group("----------beforeCreate状态----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message", this.message);
        },
        created() {
            console.group("---------created状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message", this.message);
        },
        beforeMount() {
            console.group("---------beforeMount状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message", this.message);
        },
        mounted() {
            console.group("---------mounted状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message", this.message);
        },
        beforeUpdate() {
            console.group("---------beforeUpdate状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message--data中的数据", this.message);
            console.log("页面上的数据",document.getElementById("msg").innerText);
        },
        updated() {
            console.group("---------updated状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message--data中的数据", this.message);
            console.log("页面上的数据",document.getElementById("msg").innerText);
        },
        beforeDestroy() {
            console.group("---------beforeDestroy状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message", this.message);
        },
        destroyed() {
            console.group("---------destroyed状态-----------");
            console.log("this.$el:", this.$el);
            console.log("this.$data:", this.$data);
            console.log("this.message", this.message);
        },
    })
</script>

</html>
页面初次加载时的状态
页面数据更新的状态
页面销毁的状态,实例被完全销毁。清理它与其它实例的连接,解绑它的全部指令及事件监听器。双向绑定也失效了

相关文章

  • vue3较vue2的特别之处 - 生命周期

    vue2 生命周期 vue3 生命周期 将 Vue2 的生命周期钩子代码更新到 Vue3 参考地址:https:/...

  • Vue

    Vue框架 一、Vue基础 添加Vue到网页(无需保存到本地) Vue生命周期 二、Vue 实例 创建Vue实例:...

  • 【Vue3 从入门到实战 进阶式掌握完整知识体系】002-Vue

    二、Vue语法基础 1、Vue中的应用和组件的基本部分 代码 运行结果 2、理解Vue的生命周期函数* 生命周期图...

  • vue面试必会

    一、对于MVVM的理解? 二、 Vue的生命周期 1.什么是Vue生命周期? 2.vue生命周期的作用是什么? 3...

  • 01-vue入门

    基础知识: vue的生命周期: beforeCreate/created、 beforeMount/mounted...

  • Vue 基础【3】— 生命周期

    生命周期示例图: 各个生命周期的作用 生命周期描述beforeCreate组件实例被创建之前,组件的属性生效之前c...

  • vue的生命周期

    目录:1.对于MVVM的理解2.Vue的生命周期3.什么是vue生命周期?4.vue生命周期的作用是什么?5.vu...

  • 2018-07-30 vue相关总结

    基础知识: vue的生命周期:beforeCreate/created、beforeMount/mounted、b...

  • (vue)vue知识点小结(1)

    基础知识: vue的生命周期:beforeCreate/created、beforeMount/mounted、b...

  • vue3的知识整理

    1. vue3的生命周期 vue3的生命周期一般有2种形式写法,一种是基于vue2的options API的写法,...

网友评论

      本文标题:Vue 基础【3】— 生命周期

      本文链接:https://www.haomeiwen.com/subject/omwxuktx.html