当前位置: 首页 > news >正文

初识 Vue(07)---(Vue 实例的生命周期钩子)

Vue 实例的生命周期钩子

(生命周期函数就是 Vue 实例在某一个时间点会自动执行的函数)

每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听、编译模板、将实例挂载到 DOM 并在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数,这给了用户在不同阶段添加自己的代码的机会。

比如 created 钩子可以用来在一个实例被创建之后执行代码:

new Vue({
  data: {
    a: 1
  },
  created: function () {
    // `this` 指向 vm 实例
    console.log('a is: ' + this.a)
  }
})
// => "a is: 1"

生命周期图示

图解:

步骤一

基础初始化之后,自动调用函数  beforeCreate

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 实例生命周期函数</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='app'>hello world</div>
    <script>
       var vm = new Vue({
            el:'#root',
            beforeCreate:function(){
                console.log("beforeCreate");
            }
       })  
    </script>   
</body>
</html>

输出:

当创建 Vue 实例的时候,在经过基础的初始化之后,会自动的调用生命周期函数  beforeCreate

 

步骤二

调用函数  beforeCreate 之后,会处理一些外部的注入(双向绑定),这时 Vue 的一些初始化操作基本完成;自动执行生命周期函数 Created

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 实例生命周期函数</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='app'>hello world</div>
    <script>
       var vm = new Vue({
            el:'#app',
            beforeCreate:function(){
                console.log("beforeCreate");
            },
            created:function(){
                console.log("created");
            }
       })  
    </script>   
</body>
</html>
     created:function(){
                console.log("created");
            }
       })  
    </script>   
</body>
</html>

 

输出:             

 

当执行完 beforeCreate 之后, created 也被自动执行

 

步骤三

 查询“el” 和 “template ”(模板)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 实例生命周期函数</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='app'>hello world</div>
    <script>
       var vm = new Vue({
            el:'#app',
            template:"<div>hello world</div>",
            beforeCreate:function(){
                console.log("beforeCreate");
            },
            created:function(){
                console.log("created");
            }
       })  
    </script>   
</body>
</html>
  el:'#app',
            template:"<div>hello world</div>",
            beforeCreate:function(){
                console.log("beforeCreate");
            },
            created:function(){
                console.log("created");
            }
       })  
    </script>   
</body>
</html>
 

执行完生命周期函数 Created , 询问条件--Vue 实例里是否有 “el” 这个选项,在代码中可知有 “el”选项,故选择 “yes” 这条线 ;接着询问是否有 “template ”(模板);知没有该选项,则由图知选择右边的分支,将 el 的 outerHTML 当做“template ”(模板)[ 即当Vue 实例中没有“template ”(模板);会将 el 中对应的HTML当做“template ”(模板)来进行渲染,即<div id ='app'>hello world</div>

 

步骤四

执行生命周期函数 beforeMount  和 生命周期函数 mounted

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 实例生命周期函数</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='app'>hello world</div>
    <script>
       var vm = new Vue({
            el:'#app',
            template:"<div>hello world</div>",
            beforeCreate:function(){
                console.log("beforeCreate");
            },
            created:function(){
                console.log("created");
            },
            beforeMount:function(){
                console.log(this.$el);
                console.log("beforeMount");
            },
            mounted:function(){
                console.log(this.$el);
                console.log("mounted");
            }
       })  
    </script>   
</body>
</html>
     beforeMount:function(){
                console.log(this.$el);
                console.log("beforeMount");
            },
            mounted:function(){
                console.log(this.$el);
                console.log("mounted");
            }
       })  
    </script>   
</body>
</html>

 

输出:     

 

有了模板和数据之后,并不是直接将模板渲染到页面上;在页面渲染之前,自动执行生命周期函数 beforeMount,执行完成后页面没有渲染模板结合数据生成的最终 Vue 实例里面的 DOM 元素会被挂载到页面之上,即显示“hello world”之后,自动执行生命周期函数 mounted ,执行完成后页面渲染完毕。

步骤五

执行生命周期函数 beforeDestory 和 生命周期函数 destoryed;

执行生命周期函数 beforeUpdate 和 生命周期函数updated 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 实例生命周期函数</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='app'>hello world</div>
    <script>
       var vm = new Vue({
            el:'#app',
            template:"<div>{{test}}</div>",
            data:{
                test:"hello world"
            },
            beforeCreate:function(){
                console.log("beforeCreate");
            },
            created:function(){
                console.log("created");
            },
            beforeMount:function(){
                console.log(this.$el);
                console.log("beforeMount");
            },
            mounted:function(){
                console.log(this.$el);
                console.log("mounted");
            },
            beforeDestroy:function(){
                console.log("beforeDestroy");
            },
            destroyed:function(){
                console.log("destroyed");
            },
            beforeUpdate:function(){
                consloe.log("beforeUpdate");
            },
            updated:function(){
                consloe.log("updated");
            }
       })  
    </script>   
</body>
</html>
   template:"<div>{{test}}</div>",
            data:{
                test:"hello world"
            },
            beforeCreate:function(){
                console.log("beforeCreate");
            },
            created:function(){
                console.log("created");
            },
            beforeMount:function(){
                console.log(this.$el);
                console.log("beforeMount");
            },
            mounted:function(){
                console.log(this.$el);
                console.log("mounted");
            },
            beforeDestroy:function(){
                console.log("beforeDestroy");
            },
            destroyed:function(){
                console.log("destroyed");
            },
            beforeUpdate:function(){
                consloe.log("beforeUpdate");
            },
            updated:function(){
                consloe.log("updated");
            }
       })  
    </script>   
</body>
</html>

输出:

 

    

vm.$destroy( ) 被调用时(组件被销毁时)执行生命周期函数 beforeDestory ;当调用完成后,即组件完全被销毁后,执行生命周期函数 destoryed.

当数据发生改变时,在页面渲染之前,执行生命周期函数 beforeUpdate; 当重新渲染之后 ,执行生命周期函数 updated 

补充:上文提到 8个 API,总共 11 个 生命周期函数钩子 API

9.

10.

11.

相关文章:

  • 征服 TIME_WAIT !
  • 如何给操作文档添加目录
  • 保存Hive查询结果的方法
  • Session
  • greenplum安装文档
  • 第42件事 移动App设计的11大法则
  • 解决网站访问流量过大问题
  • Java高级编程——MySql采用的算法原理
  • Nginx设置多条件判断访问控制
  • Magento搜索产品结果不精准的问题
  • spring boot slf4j日记记录配置详解
  • jsp的几种跳转比较
  • Python 给字符串进行加密,生成唯一固定长度字符串
  • JAVA 网络编程小记
  • python中异常处理--raise的使用
  • python3.6+scrapy+mysql 爬虫实战
  • (三)从jvm层面了解线程的启动和停止
  • 2017届校招提前批面试回顾
  • Debian下无root权限使用Python访问Oracle
  • es6--symbol
  • Lsb图片隐写
  • PHP的类修饰符与访问修饰符
  • python学习笔记 - ThreadLocal
  • React组件设计模式(一)
  • session共享问题解决方案
  • underscore源码剖析之整体架构
  • 关于extract.autodesk.io的一些说明
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 排序算法之--选择排序
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 如何学习JavaEE,项目又该如何做?
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • ​如何防止网络攻击?
  • ​如何在iOS手机上查看应用日志
  • ​水经微图Web1.5.0版即将上线
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • #单片机(TB6600驱动42步进电机)
  • (03)光刻——半导体电路的绘制
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (三分钟)速览传统边缘检测算子
  • (五)c52学习之旅-静态数码管
  • (转)Google的Objective-C编码规范
  • ./configure、make、make install 命令
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .net 微服务 服务保护 自动重试 Polly
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试
  • .NET和.COM和.CN域名区别
  • .net生成的类,跨工程调用显示注释
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .NET业务框架的构建
  • [145] 二叉树的后序遍历 js