Vue中關(guān)于computed計算屬性的妙用
computed
computed:相當(dāng)于method,返回function內(nèi)return的值賦值在html的DOM上。
但是多個{{}}使用了computed,computed內(nèi)的function也只執(zhí)行一次。
僅當(dāng)function內(nèi)涉及到Vue實例綁定的data的值的改變,function才會從新執(zhí)行,并修改DOM上的內(nèi)容。
computed和method的對比
<div id="example"> ? {{ message.split('').reverse().join('') }} </div>
這個是vue官網(wǎng)一直拿來作為例子的代碼。在{{}}可以很方便的放入單個表達(dá)式,但是當(dāng)一個HTML的DOM里面存在太多的表達(dá)式,程序會變得很笨重難于維護(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);
控制臺輸出的結(jié)果
執(zhí)行了computed
執(zhí)行了methods
執(zhí)行了methods
執(zhí)行了computed
執(zhí)行了methods
執(zhí)行了methods
由此可見使用computed,function只會執(zhí)行一次。當(dāng)Vue實例中綁定的data數(shù)據(jù)改變的時候,computed也相對應(yīng)的只改變一次。
相同點:
- 在以上代碼中,兩個p標(biāo)簽都會打印出同樣被反轉(zhuǎn)的Hello。
不同點:
- 使用了methods的:HTML中,每一個調(diào)用了Vue的methods的方法,都需要執(zhí)行一遍reversedMessage()這個方法;
- 而使用computed計算屬性的,只執(zhí)行一遍將結(jié)果保存在緩存中。
computed和watch的對比
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)測數(shù)據(jù)內(nèi)部的改變),但是如果使用不當(dāng)就是多此一舉了。
比如以上這個例子,使用watch監(jiān)聽firstName和lastName兩個數(shù)據(jù),其實就相當(dāng)于computed,function內(nèi)使用了Vue實例綁定的firstName和lastName。
setter
computed有g(shù)etter屬性也有setter屬性,默認(rèn)只有g(shù)etter。
這個比較簡單,貼個官網(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';//此時觸發(fā)set函數(shù)
watch
比較適合watch的場景,監(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ā)生改變,這個函數(shù)就會運行 ? ? 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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Vue運行項目報錯proxy?error:?could?not?proxy?request
這篇文章主要給大家介紹了關(guān)于如何解決Vue運行項目報錯proxy?error:could?not?proxy?request的相關(guān)資料,Proxy Error指的是代理服務(wù)器無法正確處理請求的錯誤,需要的朋友可以參考下2023-08-08詳解如何提高 webpack 構(gòu)建 Vue 項目的速度
這篇文章主要介紹了詳解如何提高 webpack 構(gòu)建 Vue 項目的速度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07vue基于Element按鈕權(quán)限實現(xiàn)方案
這篇文章主要介紹了vue基于Element按鈕權(quán)限實現(xiàn)方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04webpack4+Vue搭建自己的Vue-cli項目過程分享
這篇文章主要介紹了webpack4+Vue搭建自己的Vue-cli,對于vue-cli的強大,使用過的人都知道,極大的幫助我們降低了vue的入門門檻,感興趣的朋友跟隨腳本之家小編一起看看吧2018-08-08vue+jquery+lodash實現(xiàn)滑動時頂部懸浮固定效果
這篇文章主要為大家詳細(xì)介紹了vue+jquery+lodash實現(xiàn)滑動時頂部懸浮固定效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04vue3-print-nb實現(xiàn)頁面打印(含分頁打印)示例代碼
大多數(shù)后臺系統(tǒng)中都存在打印的需求,在有打印需求時,對前端來說當(dāng)然是直接打印頁面更容易,下面這篇文章主要給大家介紹了關(guān)于vue3-print-nb實現(xiàn)頁面打印(含分頁打印)的相關(guān)資料,需要的朋友可以參考下2024-01-01