Vue計(jì)算屬性computed與方法methods的區(qū)別及說(shuō)明
前言
在官方文檔中,給出了計(jì)算屬性的說(shuō)明與用途,也講述了計(jì)算屬性與方法的區(qū)別點(diǎn)。
本篇博客只做自己的探究記錄,以官方文檔為準(zhǔn)。
計(jì)算屬性的由來(lái)
正常來(lái)說(shuō),使用模板語(yǔ)法也能實(shí)現(xiàn)一些判斷操作,并將判斷后的數(shù)據(jù)值進(jìn)行展示。
如下:
<template>
<h1>計(jì)算屬性與方法實(shí)現(xiàn)探究</h1>
<p>Has published books:</p>
<span>{{ author.books.length > 0 ? 'Yes' : 'No' }}</span>
</template>
<script>
export default {
data(){
return{
author: {
name: 'John Doe',
books: [
'Vue 2 - Advanced Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
}
}
</script>
效果展示如下所示:

但正常開發(fā)來(lái)說(shuō),在模板語(yǔ)法中,只會(huì)用來(lái)做基本的數(shù)據(jù)展示,數(shù)據(jù)的處理需要使用放入計(jì)算屬性 computed中進(jìn)行實(shí)現(xiàn)。
如下所示:
<template>
<h1>計(jì)算屬性與方法實(shí)現(xiàn)探究</h1>
<p>Has published books:</p>
<span>{{ author.books.length > 0 ? 'Yes' : 'No' }}</span>
<hr>
<span>計(jì)算屬性:{{ checkBooks }}</span> <br>
</template>
<script>
export default {
data(){
return{
author: {
name: 'John Doe',
books: [
'Vue 2 - Advanced Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed:{
checkBooks(){
console.log("=====計(jì)算屬性=====");
return this.author.books.length > 0 ? 'Yes' : 'No';
}
}
}
</script>

由于計(jì)算屬性,屬于屬性,所以在標(biāo)簽中采取{{ }}包含時(shí),不能帶有()。
方法實(shí)現(xiàn) 計(jì)算屬性 同樣的效果
由于methods中,用來(lái)存放函數(shù)、方法,所以在計(jì)算屬性computed中的方法依舊可以用methods實(shí)現(xiàn)。
如下所示:
<template>
<h1>計(jì)算屬性與方法實(shí)現(xiàn)探究</h1>
<p>Has published books:</p>
<span>{{ author.books.length > 0 ? 'Yes' : 'No' }}</span>
<hr>
<span>計(jì)算屬性:{{ checkBooks }}</span> <br>
<hr>
<span>方法實(shí)現(xiàn):{{ checkBooks1() }}</span> <br>
</template>
<script>
export default {
data(){
return{
author: {
name: 'John Doe',
books: [
'Vue 2 - Advanced Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed:{
checkBooks(){
console.log("=====計(jì)算屬性=====");
return this.author.books.length > 0 ? 'Yes' : 'No';
}
},
// methods 中存放函數(shù)
methods:{
checkBooks1(){
console.log("*****方法實(shí)現(xiàn)*****");
return this.author.books.length > 0 ? 'Yes' : 'No';
}
}
}
</script>

由于是采取methods進(jìn)行數(shù)據(jù)的計(jì)算,所以數(shù)據(jù)的展示,需要使用()標(biāo)識(shí)調(diào)用方法。
【注意】計(jì)算屬性與方法,名稱不能相同,否則會(huì)出現(xiàn)報(bào)錯(cuò)!

計(jì)算屬性緩存 vs 方法
雖然在methods中編寫一個(gè)方法調(diào)用,與計(jì)算屬性中拋出一個(gè)計(jì)算結(jié)果值,能達(dá)到一樣的效果。
但兩者本身有很大的區(qū)別。
不同之處在于計(jì)算屬性值會(huì)基于其響應(yīng)式依賴被緩存。
這句話如何理解呢,看下面的案例:
<template>
<h1>計(jì)算屬性與方法實(shí)現(xiàn)探究</h1>
<p>Has published books:</p>
<span>{{ author.books.length > 0 ? 'Yes' : 'No' }}</span>
<hr>
<span>計(jì)算屬性:{{ checkBooks }}</span> <br>
<span>計(jì)算屬性:{{ checkBooks }}</span> <br>
<span>計(jì)算屬性:{{ checkBooks }}</span> <br>
<span>計(jì)算屬性:{{ checkBooks }}</span> <br>
<hr>
<span>方法實(shí)現(xiàn):{{ checkBooks1() }}</span> <br>
<span>方法實(shí)現(xiàn):{{ checkBooks1() }}</span> <br>
<span>方法實(shí)現(xiàn):{{ checkBooks1() }}</span> <br>
<span>方法實(shí)現(xiàn):{{ checkBooks1() }}</span> <br>
</template>
<script>
export default {
data(){
return{
author: {
name: 'John Doe',
books: [
'Vue 2 - Advanced Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
}
}
},
computed:{
checkBooks(){
console.log("=====計(jì)算屬性=====");
return this.author.books.length > 0 ? 'Yes' : 'No';
}
},
// methods 中存放函數(shù)
methods:{
checkBooks1(){
console.log("*****方法實(shí)現(xiàn)*****");
return this.author.books.length > 0 ? 'Yes' : 'No';
}
}
}
</script>
查看瀏覽器控制臺(tái)中的打印信息:

【發(fā)現(xiàn)】
當(dāng)初始數(shù)組中的數(shù)據(jù)并未變化的時(shí)候,如果采取計(jì)算屬性,在第一次做調(diào)用處理,并將第一次計(jì)算的結(jié)果值做緩存;后面多次重復(fù)調(diào)用,直接讀取的是緩存中的數(shù)據(jù)值,而不是重復(fù)計(jì)算。
但方法中,每次的調(diào)用都會(huì)重新執(zhí)行一次計(jì)算邏輯。
相比之下,方法調(diào)用總是會(huì)在重渲染發(fā)生時(shí)再次執(zhí)行函數(shù)。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示
這篇文章主要為大家詳細(xì)介紹了Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹
這篇文章主要介紹了Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Vue項(xiàng)目中實(shí)現(xiàn)無(wú)感Token刷新的示例
在前端項(xiàng)目中,Token用于用戶認(rèn)證和權(quán)限管理,文章介紹了在Vue項(xiàng)目中實(shí)現(xiàn)Token的無(wú)感刷新,包括Token刷新的原理、攔截器的應(yīng)用和處理Token過期的方法,感興趣的可以了解一下2024-11-11
vue實(shí)現(xiàn)echart餅圖legend顯示百分比的方法
本文主要介紹了vue實(shí)現(xiàn)echart餅圖legend顯示百分比的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

