<thead id="kdfuf"><font id="kdfuf"></font></thead>
<samp id="kdfuf"></samp>
    <nav id="kdfuf"><strong id="kdfuf"></strong></nav>
      中文字幕无码一区二区三区在线,久久精品人人做人人爽,国产一级内射无挡观看,十八禁在线黄色网站,日韩欧美国产另类久久久精品 ,少妇人妻偷人精品一区二区,久久午夜视频,亚洲春色AⅤ无码专区

      關(guān)于Vuex的全家桶狀態(tài)管理(二)

      2018-5-28    seo達人

      如果您想訂閱本博客內(nèi)容,每天自動發(fā)到您的郵箱中, 請點這里

      1:mutations觸發(fā)狀態(tài) (同步狀態(tài))

      <template> <p class="hello"> <h1>Hello Vuex</h1> <h5>{{count}}</h5> <p> <button @click="jia">+</button> <button @click="jian">-</button> </p> </p> </template> <script> import {mapState,mapMutations} from 'vuex' export default{
        name:'hello', //寫上name的作用是,如果你頁面報錯了,他會提示你是那個頁面報的錯,很實用 //方法三 computed: mapState([ 'count' ]),
        methods:{
         ...mapMutations([ 'jia', 'jian' ])
        }
       } </script>
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26

      2:getters計算屬性

      getter不能使用箭頭函數(shù),會改變this的指向

      在store.js添加getters

       // 計算 const getters = {
        count(state){ return state.count + 66 }
      } export default new Vuex.Store({
        state,
        mutations,
        getters
      })
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13

      //count的參數(shù)就是上面定義的state對象 
      //getters中定義的方法名稱和組件中使用的時候一定是一致的,定義的是count方法,使用的時候也用count,保持一致。 
      組件中使用

      <script> import {mapState,mapMutations,mapGetters} from 'vuex' export default{
        name:'hello',
        computed: {
         ...mapState([ 'count' ]),
         ...mapGetters([ 'count' ])
        },
        methods:{
         ...mapMutations([ 'jia', 'jian' ])
        }
       } </script>
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

      3:actions (異步狀態(tài))

      在store.js添加actions

      import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 定義常量 const state = { count: 1 } // mutations用來改變store狀態(tài) 同步狀態(tài) const mutations = {
        jia(state){
          state.count ++
        },
        jian(state){
          state.count --
        },
      } // 計算屬性 const getters = {
        count(state){ return state.count + 66 }
      } // 異步狀態(tài) const actions = {
        jiaplus(context){
          context.commit('jia') //調(diào)用mutations下面的方法
          setTimeout(()=>{
            context.commit('jian')
          },2000) alert('我先被執(zhí)行了,然后兩秒后調(diào)用jian的方法') }, jianplus(context){ context.commit('jian') }
      } export default new Vuex.Store({
        state,
        mutations,
        getters,
        actions
      })
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44

      在組件中使用

      <template> <p class="hello"> <h1>Hello Vuex</h1> <h5>{{count}}</h5> <p> <button @click="jia">+</button> <button @click="jian">-</button> </p> <p> <button @click="jiaplus">+plus</button> <button @click="jianplus">-plus</button> </p> </p> </template> <script> import {mapState,mapMutations,mapGetters,mapActions} from 'vuex' export default{
        name:'hello',
        computed: {
         ...mapState([ 'count' ]),
         ...mapGetters([ 'count' ])
        },
        methods:{ // 這里是數(shù)組的方式觸發(fā)方法 ...mapMutations([ 'jia', 'jian' ]), // 換一中方式觸發(fā)方法 用對象的方式 ...mapActions({
          jiaplus: 'jiaplus',
          jianplus: 'jianplus' })
        }
       } </script> <style scoped> h5{ font-size: 20px; color: red; } </style>
          
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48

      4:modules 模塊

      適用于非常大的項目,且狀態(tài)很多的情況下使用,便于管理

      修改store.js

      import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const state = { count: 1 } const mutations = {
        jia(state){
          state.count ++
        },
        jian(state){
          state.count --
        },
      } const getters = {
        count(state){ return state.count + 66 }
      } const actions = {
        jiaplus(context){
          context.commit('jia') //調(diào)用mutations下面的方法
          setTimeout(()=>{
            context.commit('jian')
          },2000) alert('我先被執(zhí)行了,然后兩秒后調(diào)用jian的方法') }, jianplus(context){ context.commit('jian') }
      }
      
      //module使用模塊組的方式 moduleA const moduleA = { state, mutations, getters, actions }
      
      // 模塊B moduleB const moduleB = { state: { count:108
        }
      } export default new Vuex.Store({
        modules: {
          a: moduleA,
          b: moduleB,
        }
      })
      藍藍設(shè)計www.tuitetiyu.cn )是一家專注而深入的界面設(shè)計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計、BS界面設(shè)計 、 cs界面設(shè)計 、 ipad界面設(shè)計 、 包裝設(shè)計 、 圖標定制 、 用戶體驗 、交互設(shè)計、 網(wǎng)站建設(shè) 平面設(shè)計服務(wù)

      日歷

      鏈接

      個人資料

      藍藍設(shè)計的小編 http://www.tuitetiyu.cn

      存檔

      主站蜘蛛池模板: 亚洲高清毛片一区二区| 二区三区无码视频| 国产精品拍在线观看| 欧美成人精品| 中文字幕乱码无码人妻系列蜜桃| 久久大香萑太香蕉av| 国产成人综合亚洲欧美日韩 | 农村人chinese熟女| 91精品国产91久久久久水蜜桃| 国产精品碰碰现在自在| 国产一三四2021不卡| av天堂久久天堂av| 国产精品香蕉在线观看| 亚洲中文字幕无码ⅴa| 国产精品一区二区三粉嫩| 无码偷窥清纯综合图区 | 日韩美女在线观看一区| 精品国产一二三产品价格| 亚洲Av熟妇高潮30p| 无码中文字幕va精品影院| 亚洲熟妇无码八AV在线播放| 久久久久久精品免费免费WE| 欧美区一区二| 久久精品视频18| 国内精品自在自线视频香蕉| 色噜噜久久综合伊人一本| 欧洲一区二区中文字幕| 色综合99久久久无码国产精| 日本体内she精| 国产精品麻豆中文字幕| 亚洲а∨天堂久久精品9966| 综合国产在线| 国产成人精品亚洲日本语言| 又色又爽又黄大片天天看穿着丝袜| 人妻精品久久无码区| 国产精品人成在线播放新网站| WWW亚洲色大成网络.COM| 我把我的肥岳日出水来| 老熟妇乱子伦牲交视频| 97精产国品一二三产区| 精品视频在线观自拍自拍|