Vue中關(guān)于computed計(jì)算屬性的妙用
computed
computed:相當(dāng)于method,返回function內(nèi)return的值賦值在html的DOM上。
但是多個(gè){{}}使用了computed,computed內(nèi)的function也只執(zhí)行一次。
僅當(dāng)function內(nèi)涉及到Vue實(shí)例綁定的data的值的改變,function才會(huì)從新執(zhí)行,并修改DOM上的內(nèi)容。
computed和method的對(duì)比
<div id="example"> ? {{ message.split('').reverse().join('') }} </div>
這個(gè)是vue官網(wǎng)一直拿來(lái)作為例子的代碼。在{{}}可以很方便的放入單個(gè)表達(dá)式,但是當(dāng)一個(gè)HTML的DOM里面存在太多的表達(dá)式,程序會(huì)變得很笨重難于維護(hù)。
html
<div id="app9"> ? ? 9、method與computed的區(qū)別 ? ? fullName ? ? {{fullName}} ? ? fullName2 ? ? {{fullName}} ? ? fullNameMethod ? ? {{getFullName()}} ? ? fullNameMethod2 ? ? {{getFullName()}} </div>
js
var app9 = new Vue({ ? ? el: '#app9', ? ? data: { ? ? ? ? firstName: 'Foo', ? ? ? ? lastName: 'Bar' ? ? }, ? ? methods:{ ? ? ? ? getFullName:function () { ? ? ? ? ? ? console.log("執(zhí)行了methods") ? ? ? ? ? ? return this.firstName+" " +this.lastName; ? ? ? ? } ? ? }, ? ? computed: { ? ? ? ? fullName: function () { ? ? ? ? ? ? console.log("執(zhí)行了computed") ? ? ? ? ? ? return this.firstName + ' ' + this.lastName ? ? ? ? } ? ? } }) setTimeout('app9.firstName="Foo2"',3000);
控制臺(tái)輸出的結(jié)果
執(zhí)行了computed
執(zhí)行了methods
執(zhí)行了methods
執(zhí)行了computed
執(zhí)行了methods
執(zhí)行了methods
由此可見使用computed,function只會(huì)執(zhí)行一次。當(dāng)Vue實(shí)例中綁定的data數(shù)據(jù)改變的時(shí)候,computed也相對(duì)應(yīng)的只改變一次。
相同點(diǎn):
- 在以上代碼中,兩個(gè)p標(biāo)簽都會(huì)打印出同樣被反轉(zhuǎn)的Hello。
不同點(diǎn):
- 使用了methods的:HTML中,每一個(gè)調(diào)用了Vue的methods的方法,都需要執(zhí)行一遍reversedMessage()這個(gè)方法;
- 而使用computed計(jì)算屬性的,只執(zhí)行一遍將結(jié)果保存在緩存中。
computed和watch的對(duì)比
html
<div id="demo">{{ fullName }}</div>
js
var vm = new Vue({ ? el: '#demo', ? data: { ? ? firstName: 'Foo', ? ? lastName: 'Bar', ? ? fullName: 'Foo Bar' ? }, ? watch: { ? ? firstName: function (val) { ? ? ? this.fullName = val + ' ' + this.lastName ? ? }, ? ? lastName: function (val) { ? ? ? this.fullName = this.firstName + ' ' + val ? ? } ? } })
var vm = new Vue({ ? el: '#demo', ? data: { ? ? firstName: 'Foo', ? ? lastName: 'Bar' ? }, ? computed: { ? ? fullName: function () { ? ? ? return this.firstName + ' ' + this.lastName ? ? } ? } })
watch可以監(jiān)聽數(shù)據(jù)的改變(不能監(jiān)測(cè)數(shù)據(jù)內(nèi)部的改變),但是如果使用不當(dāng)就是多此一舉了。
比如以上這個(gè)例子,使用watch監(jiān)聽firstName和lastName兩個(gè)數(shù)據(jù),其實(shí)就相當(dāng)于computed,function內(nèi)使用了Vue實(shí)例綁定的firstName和lastName。
setter
computed有g(shù)etter屬性也有setter屬性,默認(rèn)只有g(shù)etter。
這個(gè)比較簡(jiǎn)單,貼個(gè)官網(wǎng)的代碼就好了。
computed: { ? fullName: { ? ? // getter ? ? get: function () { ? ? ? return this.firstName + ' ' + this.lastName ? ? }, ? ? // setter ? ? set: function (newValue) { ? ? ? var names = newValue.split(' ') ? ? ? this.firstName = names[0] ? ? ? this.lastName = names[names.length - 1] ? ? } ? } } vm.fullName = 'John Doe';//此時(shí)觸發(fā)set函數(shù)
watch
比較適合watch的場(chǎng)景,監(jiān)聽input的輸入
<div id="watch-example"> ? <p> ? ? Ask a yes/no question: ? ? <input v-model="question"> ? </p> ? <p>{{ answer }}</p> </div>
<script src="https://unpkg.com/axios@0.12.0/dist/axios.min.js"></script> <script src="https://unpkg.com/lodash@4.13.1/lodash.min.js"></script> <script> var watchExampleVM = new Vue({ ? el: '#watch-example', ? data: { ? ? question: '', ? ? answer: 'I cannot give you an answer until you ask a question!' ? }, ? watch: { ? ? // 如果 question 發(fā)生改變,這個(gè)函數(shù)就會(huì)運(yùn)行 ? ? question: function (newQuestion) { ? ? ? this.answer = 'Waiting for you to stop typing...' ? ? ? this.getAnswer() ? ? } ? }, ? methods: { ? ? getAnswer: _.debounce( ? ? ? function () { ? ? ? ? if (this.question.indexOf('?') === -1) { ? ? ? ? ? this.answer = 'Questions usually contain a question mark. ;-)' ? ? ? ? ? return ? ? ? ? } ? ? ? ? this.answer = 'Thinking...' ? ? ? ? var vm = this ? ? ? ? axios.get('https://yesno.wtf/api') ? ? ? ? ? .then(function (response) { ? ? ? ? ? ? vm.answer = _.capitalize(response.data.answer) ? ? ? ? ? }) ? ? ? ? ? .catch(function (error) { ? ? ? ? ? ? vm.answer = 'Error! Could not reach the API. ' + error ? ? ? ? ? }) ? ? ? }, ? ? ? // 這是我們?yōu)橛脩敉V馆斎氲却暮撩霐?shù) ? ? ? //_.debounce 0.5秒內(nèi)刷新就不執(zhí)行函數(shù) ? ? ? 500 ? ? ) ? } }) </script>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Vue運(yùn)行項(xiàng)目報(bào)錯(cuò)proxy?error:?could?not?proxy?request
這篇文章主要給大家介紹了關(guān)于如何解決Vue運(yùn)行項(xiàng)目報(bào)錯(cuò)proxy?error:could?not?proxy?request的相關(guān)資料,Proxy Error指的是代理服務(wù)器無(wú)法正確處理請(qǐng)求的錯(cuò)誤,需要的朋友可以參考下2023-08-08詳解如何提高 webpack 構(gòu)建 Vue 項(xiàng)目的速度
這篇文章主要介紹了詳解如何提高 webpack 構(gòu)建 Vue 項(xiàng)目的速度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案
這篇文章主要介紹了vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04webpack4+Vue搭建自己的Vue-cli項(xiàng)目過(guò)程分享
這篇文章主要介紹了webpack4+Vue搭建自己的Vue-cli,對(duì)于vue-cli的強(qiáng)大,使用過(guò)的人都知道,極大的幫助我們降低了vue的入門門檻,感興趣的朋友跟隨腳本之家小編一起看看吧2018-08-08vue+jquery+lodash實(shí)現(xiàn)滑動(dòng)時(shí)頂部懸浮固定效果
這篇文章主要為大家詳細(xì)介紹了vue+jquery+lodash實(shí)現(xiàn)滑動(dòng)時(shí)頂部懸浮固定效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Vue+SSM實(shí)現(xiàn)圖片上傳預(yù)覽效果
這篇文章主要為大家詳細(xì)介紹了Vue+SSM實(shí)現(xiàn)圖片上傳預(yù)覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)示例代碼
大多數(shù)后臺(tái)系統(tǒng)中都存在打印的需求,在有打印需求時(shí),對(duì)前端來(lái)說(shuō)當(dāng)然是直接打印頁(yè)面更容易,下面這篇文章主要給大家介紹了關(guān)于vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)的相關(guān)資料,需要的朋友可以參考下2024-01-01