Vue生命週期鉤子函數

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script&gt;
</head>
<style>
</style>

<body>
<div id="app">
<p>{{message}}</p>
數字:
</div>
<script>
var app = new Vue({
el: "#app",
data: {
message: "中國"
},

  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:red", "el :" + this.$el);
    console.log(this.$el)
    console.log("%c%s", "color:red", "data :" + this.$data);
    console.log(this.$data)
    console.log("%c%s", "color:red", "message :" + this.message)
  },
  beforeMount: function () {
    console.group('beforeMount 掛載前狀態==============')
    console.log("%c%s", "color:red", "el :" + this.$el);
    console.log(this.$el)
    console.log("%c%s", "color:red", "data :" + this.$data);
    console.log(this.$data)
    console.log("%c%s", "color:red", "message :" + this.message)
  },
  mounted: function () {
    console.group('mounted 掛載後狀態==============')
    console.log("%c%s", "color:red", "el :" + this.$el);
    console.log(this.$el)
    console.log("%c%s", "color:red", "data :" + this.$data);
    console.log(this.$data)
    console.log("%c%s", "color:red", "message :" + this.message)
  },
  beforeUpdate: function () {
    console.group('beforeUpdate 更新前狀態==============')
    console.log("%c%s", "color:red", "el :" + this.$el);
    console.log(this.$el)
    console.log("%c%s", "color:red", "data :" + this.$data);
    console.log(this.$data)
    console.log("%c%s", "color:red", "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(this.$data)
    console.log("%c%s", "color:red", "message :" + this.message)
  },
  beforeDestroy: function () {
    console.group('beforeDestroy 銷燬前狀態==============')
    console.log("%c%s", "color:red", "el :" + this.$el);
    console.log(this.$el)
    console.log("%c%s", "color:red", "data :" + this.$data);
    console.log(this.$data)
    console.log("%c%s", "color:red", "message :" + this.message)
  },
  destroyed: function () {
    console.group('destroyed 銷燬後狀態==============')
    console.log("%c%s", "color:red", "el :" + this.$el);
    console.log(this.$el)
    console.log("%c%s", "color:red", "data :" + this.$data);
    console.log(this.$data)
    console.log("%c%s", "color:red", "message :" + this.message)
  }
})

</script>
</body>

</html>

1.程序運行,控制檯看輸出:
Vue生命週期鉤子函數

2.控制檯輸入 app.message = 'test'

Vue生命週期鉤子函數
3.控制檯輸入 app.$destroy()
Vue生命週期鉤子函數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章