二、Vue实例生命周期

一、生命周期图示

  以下是官方给出的Vue实例的生命周期图示,图中展示了11个生命周期钩子(函数)中的8个
这里写图片描述

二、生命周期函数

  生命周期函数是指vue实例在某一个时间点会自动执行的函数

1.beforeCreate&created

  在实例初始化时间以及生命周期后,依赖注入初始化之前会先调用beforeCreate函数,在依赖注入初始化之后,即实例基本创建之后会调用created函数

2.beforeMount&mounted

  在实例与挂载点结合之前会调用beforeMount函数,在结合之后会调用mounted函数

3.beforeUpdate&updated

  在实例数据发生变化进行渲染之前,会调用beforeUpdate函数,渲染之后会调用updated函数

4.beforeDestroy&destroyed

  在实例摧毁之前会调用beforeDestroy函数,摧毁之后会调用destroyed函数

<!DOCTYPE html>
<html>
<head>
    <title>vue</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">{{content}}</div>
    <script>
        var app = new Vue({
            el: '#root',
            data: {
                content: 'hello world'
            },
            beforeCreate: function () {
                console.log("beforeCreate");
            },
            created: function () {
                console.log("created");
            },
            beforeMount: function () {
                console.log(this.$el);
            },
            mounted: function () {
                console.log(this.$el);
            },
            beforeUpdate: function () {
                console.log("beforeUpdate");
            },
            updated: function () {
                console.log("updated");
            },
            beforeDestroy: function () {
                console.log("beforeDestroy");
            },
            destroyed: function () {
                console.log("destroyed");
            }
        });
    </script>
</body>
</html>

  通过在控制台中输入app.$data.content = "..."或者app.content = "…"来改变数据可以调用beforeUpdateupdate函数,输入app.$destroy()摧毁实例可以调用beforeDestroydestroyed函数
  除了上述8个vue实例生命周期钩子外,还有activateddeactivatederrorCaptured三个生命周期钩子,有兴趣的可以自行查看官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值