Vuejs監(jiān)聽(tīng)vuex中值的變化的方法示例

比如說(shuō),例如,你有一籃子水果,每次你從籃子里添加或拿走水果 ,你想顯示有關(guān)水果數(shù)量的信息,但是你也想當(dāng)籃子中數(shù)量變化的時(shí)候收到通知。

丹徒ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!

fruit-count-component.vue

  <template>
   <p>Fruits: {{ count }}</p>
  </template>
  
  <script>
  import basket from '../resources/fruit-basket'
  
  export default () {
   computed: {
    count () {
     return basket.state.fruits.length
     // Or return basket.getters.fruitsCount
     // (depends on your design decisions).
    }
   },
   watch: {
    count (newCount, oldCount) {
     // Our fancy notification (2).
     console.log(`We have ${newCount} fruits now, yaay!`)
    }
   }
  }
</script>

上述代碼,請(qǐng)注意,watch 對(duì)象中函數(shù)名必須和computed對(duì)象中的函數(shù)名匹配,在上面實(shí)例中名字是 count.

被監(jiān)視屬性的新值和舊值將作為參數(shù)傳遞到監(jiān)視回調(diào)(count函數(shù))中。

basket store 看起來(lái)像這樣:

fruit-basket.js

  import Vue from 'vue'
  import Vuex from 'vuex'
  
  Vue.use(Vuex)
  
  const basket = new Vuex.Store({
   state: {
    fruits: []
   },
   getters: {
    fruitsCount (state) {
     return state.fruits.length
    }
   }
   // Obvously you would need some mutations and actions,
   // but to make example cleaner I'll skip this part.
  })
  
  export default basket

您可以在以下資源中閱讀更多內(nèi)容:

Computed properties and watchers
API docs: computed
API docs: watch

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前文章:Vuejs監(jiān)聽(tīng)vuex中值的變化的方法示例
文章地址:http://bm7419.com/article12/jjcedc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈建站公司、虛擬主機(jī)定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)