vue 生命周期初探

vue 以后发之势加上其独有的特性(压缩后很小),轻量级的MVVM框架,目前github star已有5.94万,而react 7万。由此可见是两个非常热门前端框架。这里就vue的生命周期做个初步体验。

发现看视频,动手之后,过段时间还是会忘,所以写一篇短文以备不时之需。

先附上官网的图片:vue生命周期

生命周期的钩子函数如果使用得当,会大大增加开发效率:

生命周期实践:

为了更好的查看beforeUpdate.updated,beforeDestroy,destroy钩子函数,使用v-on绑定了点击事件,setTimeout定时5秒销毁实例,具体代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Lifecycle</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <p v-on:click="getNew">点我</p>
      <p >{{message}}</p>
    </div>
    <script type="text/javascript">
      var app = new Vue({
        el:‘#app‘,
        data:{
          message:"the running boy"
        },
        methods:{
          getNew:function(){
            this.message = ‘I have change‘;
           }
        },
        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:grey","el : " + (this.$el));
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        },
        beforeMount:function(){
          console.group("beforeMount 挂在之前");
          console.log("%c%s", "color:blue","el : " + (this.$el));
          console.log(this.$el);
          console.log("%c%s", "color:blue","data : " + this.$data);
          console.log("%c%s", "color:blue","message: " + this.message);
        },
        mounted:function(){
          console.group("mounted 挂在结束状态");
          console.log("%c%s", "color:grey","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        },
        beforeUpdate:function(){
          console.group("beforeUpdate 更新状态之前");
          console.log("%c%s", "color:blue","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:blue","data : " + this.$data);
          console.log("%c%s", "color:blue","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(‘beforeDestory 销毁前状态‘);
          console.log("%c%s", "color:grey","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        },
        destroyed:function(){
          console.group(‘destroy 销毁之后‘);
          console.log("%c%s", "color:grey","el : " + this.$el);
          console.log(this.$el);
          console.log("%c%s", "color:grey","data : " + this.$data);
          console.log("%c%s", "color:grey","message: " + this.message);
        }
      });

    setTimeout(function(){
      console.log("要销毁啦");
      app.$destroy();
    },5000);
    /*app.$destroy();*/
  </script>
  </body>
</html>

初次加载之后,查看console,看到如下:

创建之前,boforeCreate: data 和el 都为初始化,undefined。

创建之后,created: data初始化,el仍未初始化。

挂载之前,beforeMount: data el 初始化, 另外蓝色矩形框内可以看到,el内还是{{message}},这里是应用Virtual DOM 技术,在mounted挂载时再渲染数据。

挂载之后,mounted:  完成挂载。

update

点击页面页面“点我”,可得到如下输出:

蓝色方框即是,看也看到点击事件已执行。

destroy

设置定时5秒销毁,销毁之后,点击事件无效。

这么多钩子函数,怎么使用呢?我也在探索中。。。

beforeCreate: 可以写loading事件。

creaded:  在结束loading,还做一些初始化,自执行函数。

mounted:  这里发起服务器请求。得到数据,配合路由做一些事情。

beforeDestroy:  删除组件之前提示,

destroyed:  当前组件已被删除,清空相关内容

本文只写了部分生命周期的基础知识,在后续的学习中会陆续更新。

时间: 2024-08-02 02:49:24

vue 生命周期初探的相关文章

vue生命周期的介绍

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>vue生命周期</title> 6 <script src="../js/vue.js"></script> 7 <meta name="viewport" content="width=devic

vue 生命周期

一 vue的生命周期如下图所示(很清晰) 二 vue生命周期的栗子 注意触发vue的created事件以后,this便指向vue实例,这点很重要 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vue生命周期</title> <script src="../js/vue.js"></script> &

vue生命周期学习心得(下)

此文接vue生命周期学习心得(上)http://www.cnblogs.com/pengshadouble/p/7488330.html通过vue生命周期学习心得(上),大至了解了vue生命周期的8个阶段及相关钩子函数触发的时间点,这章我们通过简单的代码看一下具体的运行结果: <template> <div id="container"> <headers></headers> <router-view></router

Vue生命周期及业务场景使用

vue里的生命周期是什么? vue实例从创建到销毁的过程称之为vue的生命周期 vue的生命周期各阶段都做了什么? beforeCreate 实例创建前:这个阶段实例的data.methods是读不到的created 实例创建后:这个阶段已经完成了数据观测(data observer),属性和方法的运算, watch/event 事件回调.mount挂载阶段还没开始,$el 属性目前不可见,数据并没有在DOM元素上进行渲染beforeMount:在挂载开始之前被调用:相关的 render 函数首

vue生命周期钩子函数

转载自:https://segmentfault.com/a/1190000008879966 vue生命周期探究(一) 前言 在使用vue开发的过程中,我们经常会接触到生命周期的问题.那么你知道,一个标准的工程项目中,会有多少个生命周期勾子吗?让我们来一起来盘点一下: 根组件实例:8个 (beforeCreate.created.beforeMount.mounted.beforeUpdate.updated.beforeDestroy.destroyed) 组件实例:8个 (beforeCr

vue生命周期、钩子函数

https://segmentfault.com/a/1190000011381906    详解生命周期和钩子函数 每个vue实例再被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期. 可以看到在vue一整个的生命周期中会有很多钩子函数提供给我们在vue生命周期不同的时刻进行操作, 那么先列出所有的钩子函数,然后我们再一一详解: beforeCreate created beforeMount mounted beforeUpdate updated beforeDestroy

Vue生命周期学习

转自https://www.w3cplus.com/vue/vue-instances-and-life-cycles.html Vue实例虽然没有完全遵循MVVM模型,但Vue的设计无疑受到了它的启发. View:是看得到的,即视图,用到Vue的项目中来,它应该是"模板".也就是用来挂载Vue实例的一个DOM元素,通常在项目根目录中index.html文件中出现,比如<div id="app"></div>. Model:即模型(或数据),

vue生命周期和react生命周期对比

一 vue的生命周期如下图所示(很清晰)初始化.编译.更新.销毁 二 vue生命周期的栗子 注意触发vue的created事件以后,this便指向vue实例,这点很重要 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vue生命周期</title> <script src="../js/vue.js"></

Vue生命周期xianhjie

先来看看vue官网对vue生命周期的介绍图片 Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.销毁等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁的过程,就是生命周期. 每一个组件或者实例都会经历一个完整的生命周期,总共分为三个阶段:初始化.运行中.销毁. 实例.组件通过new Vue() 创建出来之后会初始化事件和生命周期,然后就会执行beforeCreate钩子函数,这个时候,数据还没有挂载呢,只是一个空壳,无法