Vue中computed與methods的區(qū)別詳解
Vue中computed可以用來(lái)簡(jiǎn)單的拼接需要展示的數(shù)據(jù)

computed and methods
拼接展示數(shù)據(jù)的任務(wù), 也可以用methods完成, 但當(dāng)頁(yè)面的數(shù)據(jù)變化時(shí), methods中的方法會(huì)被重新調(diào)用(產(chǎn)生不必要的性能消耗), 而methods內(nèi)的方法只有和自身有關(guān)的數(shù)據(jù)變化時(shí)才會(huì)被調(diào)用
一個(gè)簡(jiǎn)單的實(shí)例

computed只在初始化時(shí)被調(diào)用
computed只在初始化時(shí)被調(diào)用
methods會(huì)在數(shù)據(jù)變化時(shí)被調(diào)用, 即使變動(dòng)的數(shù)據(jù)與自身無(wú)關(guān)
測(cè)試源碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>computed的使用</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>
<body>
<div id="root">
</div>
<script>
var vm = new Vue({
el: "#root",
data: {
name: "zhaozhao",
age: 13,
hobby: 'Python',
nameAgeStyle: {
fontSize: "20px",
color: "#0c8ac5"
}
},
template: `<div>
<div v-bind:style="nameAgeStyle">computed方式渲染: {{nameAndAge}}</div>
<div v-bind:style="nameAgeStyle">methods 方式渲染: {{getNameAndAge()}}</div>
<br>
<input type="text" v-model="hobby">
<div>愛(ài)好: {{hobby}}</div>
<div>{{noUse()}}</div>
</div>`,
computed: {
nameAndAge: {
get(){
console.log('調(diào)用computed');
return `${this.name} ==> ${this.age}`;
}
}
},
methods: {
getNameAndAge() {
console.log('調(diào)用methods');
return `${this.name} ==> ${this.age}`;
},
noUse(){
console.log("=methods==nouse==");
}
}
})
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue中的methods、computed計(jì)算屬性和watch監(jiān)聽(tīng)屬性的使用和區(qū)別解析
- vue中的data,computed,methods,created,mounted用法及說(shuō)明
- Vue中computed屬性和watch,methods的區(qū)別
- 關(guān)于Vue的 watch、computed和methods的區(qū)別匯總
- vue.js中methods watch和computed的區(qū)別示例詳解
- Vue.js計(jì)算機(jī)屬性computed和methods方法詳解
- Vue中computed、methods與watch的區(qū)別總結(jié)
- Vue中的methods、watch、computed的區(qū)別
- 深入淺析Vue.js中 computed和methods不同機(jī)制
- 淺析Vue中method與computed的區(qū)別
- vue中計(jì)算屬性(computed)、methods和watched之間的區(qū)別
- vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié)
相關(guān)文章
詳解如何優(yōu)雅運(yùn)用Vue中的KeepAlive組件
在Vue中,KeepAlive組件是一種特殊的組件,用于緩存已經(jīng)渲染過(guò)的組件實(shí)例,本文主要為大家詳細(xì)介紹了KeepAlive組件的用法,需要的小伙伴可以參考下2023-09-09
基于Vue+Webpack拆分路由文件實(shí)現(xiàn)管理
這篇文章主要介紹了基于Vue+Webpack拆分路由文件實(shí)現(xiàn)管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
vue3中的reactive函數(shù)聲明數(shù)組方式
這篇文章主要介紹了vue3中的reactive函數(shù)聲明數(shù)組方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
解決vue?app.js/vender.js過(guò)大優(yōu)化啟動(dòng)頁(yè)
這篇文章主要為大家介紹了解決vue?app.js/vender.js過(guò)大優(yōu)化啟動(dòng)頁(yè)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Vue 使用 Mint UI 實(shí)現(xiàn)左滑刪除效果CellSwipe
這篇文章主要介紹了Vue 使用 Mint UI 實(shí)現(xiàn)左滑刪除效果CellSwipe,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
vue過(guò)濾器實(shí)現(xiàn)日期格式化的案例分析
這篇文章主要介紹了vue過(guò)濾器實(shí)現(xiàn)日期格式化的案例分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

