Vue的屬性、方法、生命周期實(shí)例代碼詳解
實(shí)例
<!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,
},
// 如果我們從服務(wù)器得到的數(shù)據(jù)并不是我們需要的,可能是上面數(shù)據(jù)的結(jié)合,這時(shí)我們可以用到一個Vue提供的一個屬性:計(jì)算屬性
// 計(jì)算屬性:可以把data里面的數(shù)據(jù)計(jì)算一下,然后返回一個新的數(shù)據(jù),它被叫做computed。
computed: {
// 可以定義函數(shù),然后返回需要的數(shù)據(jù),比如下面我們要得到上面number的平方,計(jì)算結(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>
屬性
從上面的案例可以知道,屬性可以分為計(jì)算屬性(computed)和監(jiān)聽屬性(watch)。
計(jì)算屬性有一個好處在于它有一個緩存機(jī)制,因此它不需要每次都重新計(jì)算。
監(jiān)聽屬性(監(jiān)聽器),它可以監(jiān)聽一個函數(shù)或者是一個變量。
方法(methods)
methods常調(diào)用的函數(shù)。
上面的示例中,getSqure,add,number,像這些都是我們自定義的方法。
生命周期(鉤子函數(shù))
生命周期就是從它開始創(chuàng)建到銷毀經(jīng)歷的過程。
這個生命周期也就是Vue的實(shí)例,從開始創(chuàng)建,到創(chuàng)建完成,到掛載,再到更新,然后再銷毀的一系列過程,這個官方有一個說法也叫作鉤子函數(shù)。
<script>
window.onload = () => {
const App = new Vue({
......
// 生命周期第一步:創(chuàng)建前(vue實(shí)例還未創(chuàng)建)
beforeCreate() {
// %c 相當(dāng)于給輸出結(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實(shí)例
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的屬性、方法、生命周期實(shí)例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Vue3 構(gòu)建 Web Components使用詳解
這篇文章主要為大家介紹了Vue3 構(gòu)建 Web Components使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
簡單實(shí)現(xiàn)一個vue公式編輯器組件demo
這篇文章主要介紹了輕松實(shí)現(xiàn)一個簡單的vue公式編輯器組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
使用vue-resource進(jìn)行數(shù)據(jù)交互的實(shí)例
下面小編就為大家?guī)硪黄褂胿ue-resource進(jìn)行數(shù)據(jù)交互的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
el-form的model、prop屬性和表單校驗(yàn)等使用
本文主要介紹了el-form的model、prop屬性和表單校驗(yàn)等使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
利用Vue實(shí)現(xiàn)卡牌翻轉(zhuǎn)的特效
這篇文章主要介紹了如何利用Vue實(shí)現(xiàn)一個春節(jié)抽??撁?,采用了卡牌翻轉(zhuǎn)的形式。文中的實(shí)現(xiàn)方法講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-02-02
詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

