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

首頁 > 編程 > JavaScript > 正文

Vuejs監聽vuex中值的變化的方法示例

2019-11-19 12:25:51
字體:
來源:轉載
供稿:網友

比如說,例如,你有一籃子水果,每次你從籃子里添加或拿走水果 ,你想顯示有關水果數量的信息,但是你也想當籃子中數量變化的時候收到通知。

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>

上述代碼,請注意,watch 對象中函數名必須和computed對象中的函數名匹配,在上面實例中名字是 count.

被監視屬性的新值和舊值將作為參數傳遞到監視回調(count函數)中。

basket store 看起來像這樣:

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

您可以在以下資源中閱讀更多內容:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长岭县| 乳山市| 明水县| 梁河县| 宜黄县| 柳河县| 惠来县| 阿鲁科尔沁旗| 诏安县| 博白县| 宣城市| 株洲县| 南昌市| 长丰县| 呼和浩特市| 惠州市| 大荔县| 新郑市| 古田县| 星座| 东山县| 常山县| 高唐县| 揭阳市| 洛扎县| 孟连| 读书| 安岳县| 桦甸市| 奉化市| 徐州市| 天祝| 麻城市| 曲沃县| 新闻| 西丰县| 泰来县| 惠州市| 石楼县| 同仁县| 松潘县|