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="Vue.min.js"></script> </head> <body> <div id="main"> <span>{{ message }}</span> <br/> <span>{{ number }}</span> <br/> <button v-on:click="add">add</button> </div> </body> </html> <script> const App = new Vue({ // 選擇器 el: '#main', // 數(shù)據(jù) data: { // 在data里面不僅可以定義字符串,我們還可以定義number message: 'Welcome to Chivalrous Island!', number: 85, }, // 如果我們從服務器得到的數(shù)據(jù)并不是我們需要的,可能是上面數(shù)據(jù)的結(jié)合,這時我們可以用到一個Vue提供的一個屬性:計算屬性 // 計算屬性:可以把data里面的數(shù)據(jù)計算一下,然后返回一個新的數(shù)據(jù),它被叫做computed。 computed: { // 可以定義函數(shù),然后返回需要的數(shù)據(jù),比如下面我們要得到上面number的平方,計算結(jié)果為: getSqure: function () { return this.number * this.number; } }, // 定義函數(shù) methods: { add: function() { this.number++; } }, // 監(jiān)聽屬性(監(jiān)聽器),它可以監(jiān)聽一個函數(shù)或者是一個變量 watch: { // 函數(shù)接收兩個參數(shù)值,afterVal代表改變之后的值,beforeVal表示改變之前的值 number: function(afterVal,beforeVal) { console.log('beforeVal',beforeVal); console.log('afterVal',afterVal); } } }); // 打印出來的結(jié)果 console.log(App.getSqure); </script>
屬性
從上面的案例可以知道,屬性可以分為計算屬性(computed)和監(jiān)聽屬性(watch)。
計算屬性有一個好處在于它有一個緩存機制,因此它不需要每次都重新計算。
監(jiān)聽屬性(監(jiān)聽器),它可以監(jiān)聽一個函數(shù)或者是一個變量。
方法(methods)
methods常調(diào)用的函數(shù)。
上面的示例中,getSqure,add,number,像這些都是我們自定義的方法。
生命周期(鉤子函數(shù))
生命周期就是從它開始創(chuàng)建到銷毀經(jīng)歷的過程。
這個生命周期也就是Vue的實例,從開始創(chuàng)建,到創(chuàng)建完成,到掛載,再到更新,然后再銷毀的一系列過程,這個官方有一個說法也叫作鉤子函數(shù)。
<script> window.onload = () => { const App = new Vue({ ...... // 生命周期第一步:創(chuàng)建前(vue實例還未創(chuàng)建) beforeCreate() { // %c 相當于給輸出結(jié)果定義一個樣式 console.log('%cbeforeCreate','color:green', this.$el); console.log('%cbeforeCreate','color:green', this.message); }, // 創(chuàng)建完成 created() { console.log('%ccreated','color:red', this.$el); console.log('%ccreated','color:red', this.message); }, // 掛載之前 beforeMount() { console.log('%cbeforeMount','color:blue', this.$el); console.log('%cbeforeMount','color:blue', this.message); }, // 已經(jīng)掛載但是methods里面的方法還沒有執(zhí)行,從創(chuàng)建到掛載全部完成 mounted() { console.log('%cmounted','color:orange', this.$el); console.log('%cmounted','color:orange', this.message); }, // 創(chuàng)建完之后,數(shù)據(jù)更新前 beforeUpdate() { console.log('%cbeforeUpdate','color:#f44586', this.$el); console.log('%cbeforeUpdate','color:#f44586', this.number); }, // 數(shù)據(jù)全部更新完成 updated() { console.log('%cupdated','color:olive', this.$el); console.log('%cupdated','color:olive', this.number); }, // 銷毀 beforeDestroy() { console.log('%cbeforeDestroy','color:gray', this.$el); console.log('%cbeforeDestroy','color:gray', this.number); }, destroyed() { console.log('%cdestroyed','color:yellow', this.$el); console.log('%cdestroyed','color:yellow', this.number); } }); // 打印出來的結(jié)果 console.log(App.getSqure); window.App = App; }; // 銷毀vue實例 function destroy() { App.$destroy(); } </script>
html:
<body> <div id="main"> <span>{{ message }}</span> <br/> <span>{{ number }}</span> <br/> <button v-on:click="add">add</button> <br /> <button Onclick="destroy()">destroy</button> </div> </body>
總結(jié)
以上所述是小編給大家介紹的Vue的屬性、方法、生命周期實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關文章
Vue中router-link如何添加mouseover提示
這篇文章主要介紹了Vue中router-link如何添加mouseover提示,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11vue2中組件互相調(diào)用實例methods中的方法實現(xiàn)詳解
vue在同一個組件內(nèi),方法之間經(jīng)常需要互相調(diào)用,下面這篇文章主要給大家介紹了關于vue2中組件互相調(diào)用實例methods中的方法實現(xiàn)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08