computed使用詳解(附demo,不定期更新)

computed

核心: 依賴緩存而改變

基礎(chǔ)用法:1.get:依賴緩存,監(jiān)測(cè)data數(shù)據(jù)的變化;2.set:監(jiān)測(cè)computed本身數(shù)據(jù)的變化 。3,關(guān)閉cache:每次訪問都是最新的而非緩存;
注意:vuex傳遞的computed值必須通過watch才能監(jiān)測(cè)到,set函數(shù)無(wú)法監(jiān)測(cè)到

實(shí)現(xiàn)用computed代替watch的地方:

  • 1.data => computed

    data數(shù)據(jù)改變,computed因改變而改變;而不會(huì)用watch; √
    監(jiān)聽data的變化

demo

<template>
  <div>
    <h4>測(cè)試computed</h4>
    <div>
      <input type="text" v-model="message" />
      <span>{{defdata}}</span>
    </div>
    <div>setter</div>

  </div>

</template>

<script>
   export default {
    data () {
       return {
         message: 'hello'
       }
     },
    computed: {
       def () {
         return 'dfd'
       },
       defdata () {
         console.log(this.message) // message被改變的時(shí)候,這里會(huì)被執(zhí)行;并重新賦值defdata變量
         return 'dfd' +this.message
       }
     }
  }
</script>
  • 2.Computed => data ×

    computed不能監(jiān)聽computed的變化(注意:即使使用set,也改變不了computed的值,只能改變data;)
    監(jiān)聽computd的變化用set/watch

demo computed不能監(jiān)聽computed的變化

<template>
  <div>
    <h4>測(cè)試computed</h4>
    <div>
      <input type="text" v-model="abc" />
      <span>{{def}}</span>
    </div>
    <div>setter</div>
  </div>

</template>

<script>
   export default {
    data () {
       return {
         message: 'hello'
       }
     },
    computed: {
       abc () {
         return 'abc'
       },
       def () {
         console.log(this.abc)  // 這里不會(huì)被執(zhí)行并重新賦值def變量
         return 'dfd' +this.abc
       }
     }
  }
</script>
  • 3.computed的get是設(shè)置監(jiān)聽data的數(shù)據(jù);
<template>
  <div>
    <h4>測(cè)試computed的get</h4>
    <div>
      <input type="text" v-model="message" />
      <div>{{abc}}</div>
    </div>
  </div>

</template>

<script>
   export default {
    data () {
       return {
         message: 'hello'
       }
     },
    computed: {
       abc: {
         get () {
           // 監(jiān)聽data的變化,每次變化都會(huì)打印123
           console.log(123)
           // abc隨著    data中的message的變化而變化
           return 'abc' + this.message
         }
       }
     }
  }
</script>

<style>
</style>

4.computed的set函數(shù)
set函數(shù)當(dāng)computed屬性重新賦值后,set里的操作會(huì)被調(diào)用。
注:需要改變computed的值的時(shí)候,就需要設(shè)置set函數(shù)

<template>
  <div>
    <h4>測(cè)試computed的set</h4>
    <div>
      <input type="text" v-model="abc" />
    </div>
  </div>

</template>

<script>
   export default {
    data () {
       return {
         message: 'hello'
       }
     },
    computed: {
       abc: {
         get () {
           return 'abc'
         },
         set (val) {
           //  只能在set函數(shù)里面打印改變后的值
           console.log('set', val)  // => 123
         }
       }
     },
     mounted () {
       this.abc = '123'
       // 在mounted 無(wú)法打印改變后的值
       console.log('mounted', this.abc) //=> abc
     }
  }
</script>

<style>
</style>
    1. Cache :true 默認(rèn)開啟計(jì)算屬性的緩存; 如果需要每次訪問都是最新的則關(guān)閉緩存false;
      demo
<template>
  <div>
    <h4>測(cè)試computed的緩存</h4>
    <div>{{now}}</div>
  </div>

</template>

<script>
   export default {
    data () {
       return {
         message: 'hello'
       }
     },
    computed: {
      now:{
        get:function(){
          return Date.now() + this.message
        },
        cache: true   // 默認(rèn)是true
      }
    },
    mounted () {
      for(let i = 0 ;i<3;i++){
        console.log(this.now)  // 當(dāng)false關(guān)閉緩存的時(shí)候,每次訪問now是都是最新的的。 而為true的時(shí)候,則需要依賴的屬性改變了才會(huì)更新時(shí)間;
      }
    }
  }
</script>

<style>
</style>
    1. watch能用而computed的set不能用的地方;
      vuex 接收 的computed ,用set監(jiān)測(cè)不到變化,必須要用watch才可以生效;(原理:實(shí)質(zhì)沒有改變computd的值,只是改變了get的return值)
      v-model 改變的computed,用watch監(jiān)測(cè)不到變化,必須要用computed對(duì)象中的set函數(shù)方法才能監(jiān)測(cè)得到(原理:相當(dāng)于每次改變了computed的值)
  1. 寫法規(guī)范:
    vuex 傳遞的computed值
computed: {
     abc  () {
          return 'vuex
    }
 }

自身computed定義

computed: {
    abc: {
      get () {
          return 'computed'
      }
    }
}
參考鏈接
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容