Vue 生命周期(钩子函数)

2020-11-27  本文已影响0人  暖A暖

什么是生命周期

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

下图是官网中实例的生命周期示例图:

示例:

我们结合代码去看钩子函数的执行,例如下面这段代码:

<!DOCTYPE html>
<html>
<head>
    <title>Vue的属性、方法和生命周期_侠课岛(9xkd.com)</title>
    <script src="./src/vue.min.js" type="text/javascript" ></script>
</head>
<body>

<div id="app">
     <p>{{ message }}</p>
     <input type="button" @click="change" value="更新数据" />
     <input type="button" @click="destroy" value="销毁" />
</div>

<script type="text/javascript">
    
  var app = new Vue({
       el: '#app',
       data: {
          message : "侠课岛欢迎你" 
       },
       methods:{
            change() {
                this.message = '好好学习,天天向上';
            },
            destroy() {
                app.$destroy();
            }
        },
       beforeCreate: function () {
            console.group('beforeCreate 创建前状态:');
            console.log("%c%s", "color:gary" , "el     : " + this.$el); 
            console.log("%c%s", "color:gary","message: " + this.message)  
       },
       created: function () {
            console.group('created 创建完毕状态:');
            console.log("%c%s", "color:green","el     : " + this.$el); 
            console.log("%c%s", "color:green","message: " + this.message); 
       },
       beforeMount: function () {
            console.group('beforeMount 挂载前状态:');
            console.log("%c%s", "color:gary","el     : " + (this.$el)); 
            console.log("%c%s", "color:gary","message: " + this.message); 
       },
       mounted: function () {
            console.group('mounted 挂载结束状态:');
            console.log("%c%s", "color:orange","el     : " + this.$el); 
            console.log("%c%s", "color:orange","message: " + this.message);
       },
       beforeUpdate: function () {
            console.group('beforeUpdate 更新前状态:');
            console.log("%c%s", "color:gary","el     : " + this.$el);
            console.log("%c%s", "color:gary","message: " + this.message); 
       },
       updated: function () {
            console.group('updated 更新完成状态:');
            console.log("%c%s", "color:Violet","el     : " + this.$el);
            console.log("%c%s", "color:Violet","message: " + this.message); 
       },
        beforeDestroy: function () {
            console.group('beforeDestroy 销毁前状态:');
            console.log("%c%s", "color:gary","el     : " + this.$el);
            console.log("%c%s", "color:gary","message: " + this.message); 
        },
        destroyed: function () {
            console.group('destroyed 销毁完成状态:');
            console.log("%c%s", "color:blue","el     : " + this.$el);
            console.log("%c%s", "color:blue","message: " + this.message)
        }
    })
</script>
</body>
</html>

在浏览器中打开:

点击更新数据按钮:

点击销毁按钮:

钩子函数

一个指令定义对象可以提供如下几个钩子函数,这些钩子函数都是可选:

钩子函数参数

指令钩子函数会被传入以下参数:

钩子函数使用

下面是一个自定义钩子使用:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue的学习_侠课岛(9xkd.com)</title>
<script src="./src/vue.min.js"></script>
</head>
<body>
  <div id="app">
    <my-comp v-if="message" :msg="message"></my-comp>
    <button @click="change">更改</button>
  </div>
</body>
<script>
  Vue.directive('hello', {
    bind: function (el) {
      console.log('bind')
    },
    inserted: function (el) {
      console.log('inserted')
    },
    update: function (el) {
      console.log('update')
    },
    componentUpdated: function (el) {
      console.log('componentUpdated')
    },
    unbind: function (el) {
      console.log('unbind')
    }
  })
  var myComp = {
    template: '<h1 v-hello>{{msg}}</h1>',
    props: {
      msg: String
    }
  }
  new Vue({
    el: '#app',
    data(){
      return{
        message:"侠课岛"
      }
    },
    methods:{
      change: function () {
          this.message = '小飞侠'
      }
    },
    components: {
      myComp: myComp
    },
  });
</script>
</html>

演示效果:


上一篇下一篇

猜你喜欢

热点阅读