国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue計算屬性的學(xué)習(xí)筆記

2019-11-19 17:04:58
字體:
供稿:網(wǎng)友

本文為大家分享了Vue計算屬性的學(xué)習(xí)筆記,供大家參考,具體內(nèi)容如下

①模板內(nèi)的表達(dá)式實(shí)際上只用于簡單的運(yùn)算,對于復(fù)雜邏輯,使用計算機(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('')  }  } }) 

這里我們聲明了一個計算機(jī)屬性reversedMessage,我們提供的函數(shù)將用作屬性vm.reversedMessage的getter。

③計算機(jī)緩存 vs Methods

可以通過調(diào)用表達(dá)式中的method來達(dá)到同樣的效果:

<p>Reversed message:"{{reversedMessage}}"</p> 
//in component methods:{  reversedMessage:function(){  return this.message.split('').reverse()/join('')  } } 

可以將同一個函數(shù)定義為一個method而不是一個計算機(jī)屬性。對于最終的結(jié)果,兩種方式確實(shí)是相同的。然而不同的計算機(jī)屬性是基于它們的依賴進(jìn)行緩存的。計算屬性只有在它的相關(guān)依賴發(fā)生改變時才會重新求值,這就意味著只要message還沒有改變,多次訪問reversedMessage計算屬性會立即返回之前的計算結(jié)果,而不必再次執(zhí)行函數(shù)。
下面的計算屬性將不再更新,因?yàn)镈ate.now()不是響應(yīng)式依賴:

computed:{  now:function(){  return Date.now()  } } 

只要發(fā)生重新渲染,method調(diào)用總會執(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  }  } }) 

⑤計算setter:

計算屬性默認(rèn)只有g(shù)etter,不過在需要是可以提供一個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'時,setter會被調(diào)用,vm.firstName和vm.lastName  也相應(yīng)的會被更新。

⑥觀察watchers

    當(dāng)想要在數(shù)據(jù)變化相應(yīng)時,執(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ā)生改變,這個函數(shù)就會運(yùn)行  question: function (newQuestion) {   this.answer = 'Waiting for you to stop typing...'   this.getAnswer()  }  },  methods: {  // _.debounce 是一個通過 lodash 限制操作頻率的函數(shù)。  // 在這個例子中,我們希望限制訪問yesno.wtf/api的頻率  // ajax請求直到用戶輸入完畢才會發(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>

 在這個示例中,使用watch選項(xiàng)允許我們執(zhí)行異步操作,限制我們執(zhí)行該操作的頻率,并在得到最終結(jié)果前,設(shè)置中間狀態(tài),這是計算屬性無法做到的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万荣县| 化州市| 巴青县| 济源市| 额济纳旗| 牟定县| 从化市| 墨竹工卡县| 阜新| 巴里| 长垣县| 曲水县| 梧州市| 苍山县| 太谷县| 柞水县| 张家港市| 耒阳市| 贵定县| 民县| 延安市| 双城市| 平武县| 石楼县| 淳安县| 江源县| 贵阳市| 宣化县| 伊金霍洛旗| 台中县| 延庆县| 西和县| 黄龙县| 海门市| 盱眙县| 长乐市| 泌阳县| 招远市| 都匀市| 钟祥市| 宜春市|