vue 生命周期钩子函数

生命周期

每个 Vue 实例在被创建时都要经过一系列的初始化过程 :创建实例,装载模板,渲染模板等等。Vue 为生命周期中的每个状态都设置了钩子函数(监听函数)。每当 Vue 实例处于不同的生命周期时,对应的函数就会被触发调用。
在这里插入图片描述

钩子函数

 beforeCreated:我们在用 Vue 时都要进行实例化,因此,该函数就是在 Vue 实例化时调
用,也可以将他理解为初始化函数比较方便一点,在 Vue1.0 时,这个函数的名字就是
init。
 created:在创建实例之后进行调用。
 beforeMount:页面加载完成,没有渲染。如:此时页面还是{{name}}
 mounted:我们可以将他理解为原生 js 中的 window.οnlοad=function({.,.}),或许大家也在
用 jquery,所以也可以理解为 jquery 中的$(document).ready(function(){….}),他的功能就
是:在 dom 文档渲染完毕之后将要执行的函数,该函数在 Vue1.0 版本中名字为
compiled。 此时页面中的{{name}}已被渲染成张三
 beforeDestroy:该函数将在销毁实例前进行调用 。
 destroyed:改函数将在销毁实例时进行调用。
 beforeUpdate:组件更新之前。
 updated:组件更新之后。

<body>
    <div id="app">
        <span id="num">{{num}}</span>
        <button v-on:click="num++">点赞</button>
        <h2>
            {{name}},有{{num}}个人点赞。
        </h2>
    </div>
    <script>
        let app = new Vue({
            el: "#app",
            data: {
                name: "张三",
                num: 100
            },
            methods: {
                show() {
                    return this.name;
                },
                add() {
                    this.num++;
                }
            },
            beforeCreate() {
                console.log("=========beforeCreate=============");
                console.log("数据模型未加载:" + this.name, this.num);
                console.log("方法未加载:" + this.show());
                console.log("html 模板未加载:" + document.getElementById("num"));
            },
            created: function () {
                console.log("=========created=============");
                console.log("数据模型已加载:" + this.name, this.num);
                console.log("方法已加载:" + this.show());
                console.log("html 模板已加载:" + document.getElementById("num"));
                console.log("html 模板未渲染:" + document.getElementById("num").innerText);
            },
            beforeMount() {
                console.log("=========beforeMount=============");
                console.log("html 模板未渲染:" + document.getElementById("num").innerText);
            },
            mounted() {
                console.log("=========mounted=============");
                console.log("html 模板已渲染: " + document.getElementById("num").innerText);
            },
            beforeUpdate() {
                console.log("=========beforeUpdate=============");
                console.log("数据模型已更新:" + this.num);
                console.log("html 模板未更新:" + document.getElementById("num").innerText);
            },
            updated() {
                console.log("=========updated=============");
                console.log("数据模型已更新:" + this.num);
                console.log("html 模板已更新:" + document.getElementById("num").innerText);
            }
        });
    </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值