vue.js基礎__Vue的生命周期(鈎子函數)


Vue的生命周期,也就是鈎子函數;Vue一共有10個生命周期函數,

我們可以利用這些函數在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>Vue的生命周期</title>
<script src="../assets/js/vue.js"></script>
</head>

<body>
<h1>Vue的生命周期</h1>
<hr>
<div id="app">
{{count}}
<p><button @click="add">add</button></p>
</div>
<button onclick="app.$destroy()">destroy</button>

<script>
var app = new Vue({
el: '#app',
data: {
count: 1
},
methods: {
add: function () {
this.count++
}
},
beforeCreate() {
console.log('1 - beforeCreate 初始化之后')
},
created() {
console.log('2 - created 創建完成')
},
beforeMount() {
console.log('3 - beforeMount 掛載之前')
},
mounted() {
console.log('4 - mounted 被掛載之后')
},
beforeUpdate() {
console.log('5 - beforeUpdate 數據更新前')
},
updated() {
console.log('6 - updated 被更新后')
},
activated() {
console.log('7 - activated')
},
deactivated() {
console.log('8 - deactivated')
},
beforeDestroy() {
console.log('9 - beforeDestroy 銷毀之前')
},
destroyed() {
console.log('10 - destroyed 銷毀之后')
},
})
</script>
</body>

</html>

 

運行以上代碼可以看出,1,2,3,4首先進行加載,點擊后5,6加載,當點擊銷毀時,9,10進行加載;

在vue 中生命周期函數,也就是鈎子函數,還是非常常用的,因為是單頁面應用,

所以需要加載大量資源和圖片


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM