最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中的計算屬性和偵聽屬性

 更新時間:2020年11月06日 09:03:56   作者:LC蝸牛  
這篇文章主要介紹了vue中的計算屬性和偵聽屬性,幫助大家更好的理解和學(xué)習(xí)vue框架的使用,感興趣的朋友可以了解下

計算屬性

計算屬性用于處理復(fù)雜的業(yè)務(wù)邏輯

計算屬性具有依賴性,計算屬性依賴 data中的初始值,只有當(dāng)初始值改變的時候,計算屬性才會再次計算

計算屬性一般書寫為一個函數(shù),返回了一個值,這個值具有依賴性,只有依賴的那個值發(fā)生改變,他才會重新計算

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>表單輸入綁定</title>
</head>
<body>
 <div id="app">

  {{ reverseMsg }}---- {{ reverseMsg }}-------- {{ reverseMsg }} //直接使用計算屬性中的函數(shù)就是所要的數(shù)據(jù)

 </div>
</body>
<script src="vue.js"></script> //vue的js,不然使用不了vue語法
<script>
 const app = new Vue({
  el: '#app',
  data: {
   msg: 'hello world'
  },
  computed: {
   reverseMsg () { // 計算屬性是一個函數(shù),返回一個值,使用和data中的選項一樣
    console.log('computed') // 執(zhí)行1次 --- 依賴性
    return this.msg.split('').reverse().join('');
   }
  }
 })
</script>
</html>

偵聽屬性(監(jiān)聽屬性)

vue提供了檢測數(shù)據(jù)變化的一個屬性 watch 可以通過  newVal 獲取變化之后的值

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>表單輸入綁定</title>
</head>
<body>
 <div id="app">
  <input type="text" v-model="firstname"> + <input type="text" v-model="lastname"> = {{ fullname }}

 </div>
</body>
<script src="vue.js"></script>
<script>
 const app = new Vue({
  el: '#app',
  data: {
   firstname: '李',
   lastname: '小龍',
   fullname: '李小龍'
  },
  watch: { // 偵聽屬性
   firstname (newVal, oldVal) { // newVal變化之后的值
    this.fullname = newVal + this.lastname // 當(dāng)改變 姓 的時候執(zhí)行
   },
   lastname (newVal, oldVal) {
    this.fullname = this.firstname + newVal // 當(dāng)改變 名字 的時候執(zhí)行
   }
  }
 })
</script>
</html>

以上就是vue中的計算屬性和偵聽屬性的詳細(xì)內(nèi)容,更多關(guān)于vue 計算屬性和偵聽屬性的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

东莞市| 高安市| 济源市| 墨竹工卡县| 什邡市| 普宁市| 盐池县| 神木县| 镇安县| 岳西县| 临沧市| 河源市| 嘉荫县| 天峻县| 绥中县| 瑞安市| 容城县| 黎川县| 黄大仙区| 沂南县| 凤凰县| 宜兰市| 屯留县| 宁化县| 宁津县| 静乐县| 冀州市| 寻乌县| 宁蒗| 涿鹿县| 岚皋县| 乡城县| 安阳市| 阳城县| 安陆市| 鹤峰县| 延边| 长兴县| 开阳县| 柏乡县| 延安市|