1. 程式人生 > >vue生命週期函式

vue生命週期函式

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生命週期</title>
</head>
<body>
<div id="app">
<!--hello world-->
</div>
<script src="js/vue.js"></script>
<script>
//生命週期函式就是vue例項在某一個時間點會自動執行的函式
var vm = new Vue({
   el:'#app',
   template:"<div>{{test}}</div>",
   data:{
   test:"hello world"
   },
   beforeCreate:function() {
//當建立Vue例項的時候,當Vue例項進行基礎的初始化之後就會自動呼叫這個函式
   console.log("beforeCreate");
   },
   created:function(){
   console.log("created");
   },
   //當函式進行處理外部注入和繫結相關內容時進行觸發,vue的初始化都完成了之後,函式created會被執行.
//   是否存在el屬性,是則會詢問是否存在templetes模板屬性,不存在則,則會吧el外層的html作為模板
beforeMount:function(){
console.log(this.$el);//el指最外層的元素即包括裡面的內容
console.log("beforeMount");
},
//在把文字渲染到模板之前執行beforeMount函式
mounted:function(){//當文字已經被渲染到頁面 上的時候執行mounted函式
console.log(this.$el);
console.log("mounted");
},
//如何判斷beforeMount和mounted是誰先執行呢
//在beforeMount之前裡輸入console.log("this.$el");
beforeDestroy:function(){
console.log("beforeDestroy");
},
destroyed:function  () {
console.log("destroyed");
},
beforeUpdate:function(){  //資料發生改變還沒有渲染之前,執行
console.log("beforeUpdate");
},
updated:function(){//重新渲染之後updated會被執行
console.log("updated");
}

})
//vue生命週期並不放在methods方法裡
</script>

</body>

</html>