Vue計(jì)算屬性的學(xué)習(xí)筆記
本文為大家分享了Vue計(jì)算屬性的學(xué)習(xí)筆記,供大家參考,具體內(nèi)容如下
①模板內(nèi)的表達(dá)式實(shí)際上只用于簡(jiǎn)單的運(yùn)算,對(duì)于復(fù)雜邏輯,使用計(jì)算機(jī)屬性。
②基礎(chǔ)例子:
<div id = "example">
<p>Original message:"{{message}}"</p>
<p>Computed reversed message:"{{reversedMessage}}"</p>
</div>
var vm = new Vue({
el:"#example",
data:{
message:"Hello"
},
computed:{
//a computed getter
reversedMessage:function(){
//'this' points to the vm instance
return this.message.split('').reverse().join('')
}
}
})
這里我們聲明了一個(gè)計(jì)算機(jī)屬性reversedMessage,我們提供的函數(shù)將用作屬性vm.reversedMessage的getter。
③計(jì)算機(jī)緩存 vs Methods
可以通過調(diào)用表達(dá)式中的method來達(dá)到同樣的效果:
<p>Reversed message:"{{reversedMessage}}"</p>
//in component
methods:{
reversedMessage:function(){
return this.message.split('').reverse()/join('')
}
}
可以將同一個(gè)函數(shù)定義為一個(gè)method而不是一個(gè)計(jì)算機(jī)屬性。對(duì)于最終的結(jié)果,兩種方式確實(shí)是相同的。然而不同的計(jì)算機(jī)屬性是基于它們的依賴進(jìn)行緩存的。計(jì)算屬性只有在它的相關(guān)依賴發(fā)生改變時(shí)才會(huì)重新求值,這就意味著只要message還沒有改變,多次訪問reversedMessage計(jì)算屬性會(huì)立即返回之前的計(jì)算結(jié)果,而不必再次執(zhí)行函數(shù)。
下面的計(jì)算屬性將不再更新,因?yàn)镈ate.now()不是響應(yīng)式依賴:
computed:{
now:function(){
return Date.now()
}
}
只要發(fā)生重新渲染,method調(diào)用總會(huì)執(zhí)行該函數(shù)。
④computed屬性 vs watch屬性
<div id= "demo">{{fullName}}</div>
watch:
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
}
}
})
computed:
var vm = new Vue({
el:'#demo',
data:{
firstName:'Foo',
lastName:'Bar'
},
computed:{
fullName:function(){
return this.firstName + ' ' + this.lastName
}
}
})
⑤計(jì)算setter:
計(jì)算屬性默認(rèn)只有g(shù)etter,不過在需要是可以提供一個(gè)setter:
// ...
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]
}
}
}
// ...
在運(yùn)行vm.fullName = 'John Doe'時(shí),setter會(huì)被調(diào)用,vm.firstName和vm.lastName 也相應(yīng)的會(huì)被更新。
⑥觀察watchers
當(dāng)想要在數(shù)據(jù)變化相應(yīng)時(shí),執(zhí)行異步操作或開銷較大的操作,這是很有用的。
<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: {
// _.debounce 是一個(gè)通過 lodash 限制操作頻率的函數(shù)。
// 在這個(gè)例子中,我們希望限制訪問yesno.wtf/api的頻率
// ajax請(qǐng)求直到用戶輸入完畢才會(huì)發(fā)出
// 學(xué)習(xí)更多關(guān)于 _.debounce function (and its cousin
// _.throttle), 參考: https://lodash.com/docs#debounce
getAnswer: _.debounce(
function () {
var vm = this
if (this.question.indexOf('?') === -1) {
vm.answer = 'Questions usually contain a question mark. ;-)'
return
}
vm.answer = 'Thinking...'
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ù)
500
)
}
})
</script>
在這個(gè)示例中,使用watch選項(xiàng)允許我們執(zhí)行異步操作,限制我們執(zhí)行該操作的頻率,并在得到最終結(jié)果前,設(shè)置中間狀態(tài),這是計(jì)算屬性無法做到的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue.js實(shí)現(xiàn)價(jià)格計(jì)算器功能
- Vue.js每天必學(xué)之計(jì)算屬性computed與$watch
- Vue computed計(jì)算屬性的使用方法
- Vue計(jì)算屬性的使用
- Vue.js計(jì)算屬性computed與watch(5)
- Vue.js教程之計(jì)算屬性
- vue實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)匯率計(jì)算功能
- Vue中計(jì)算屬性computed的示例解讀
- vue2.0中vue-cli實(shí)現(xiàn)全選、單選計(jì)算總價(jià)格的實(shí)例代碼
- vue中的計(jì)算屬性的使用和vue實(shí)例的方法示例
- vue.js入門教程之計(jì)算屬性
- Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例
相關(guān)文章
vue與django(drf)實(shí)現(xiàn)文件上傳下載功能全過程
最近簡(jiǎn)單的學(xué)習(xí)了django和drf上傳文件(主要是圖片),做一個(gè)記錄,下面這篇文章主要給大家介紹了關(guān)于vue與django(drf)實(shí)現(xiàn)文件上傳下載功能的相關(guān)資料,需要的朋友可以參考下2023-02-02
vue el-form-item如何添加icon和tooltip
這篇文章主要介紹了vue el-form-item如何添加icon和tooltip問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
使用Vue自定義數(shù)字鍵盤組件(體驗(yàn)度極好)
最近做 Vue 開發(fā),因?yàn)橛胁簧夙撁嫔婕暗浇痤~輸入,產(chǎn)品老是覺得用原生的 input 進(jìn)行金額輸入的話 體驗(yàn)很不好,于是自己動(dòng)手寫了一個(gè)使用Vue自定義數(shù)字鍵盤組件,具體實(shí)現(xiàn)代碼大家參考下本文2017-12-12
vue項(xiàng)目報(bào)錯(cuò)Extra?semicolon?(semi)問題及解決
這篇文章主要介紹了vue項(xiàng)目報(bào)錯(cuò)Extra?semicolon?(semi)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

