欧美videos另类精品-欧美videos另类极品-欧美vide-欧美va在线视频-欧美va在线观看-欧美va在线播放免费观看

關于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,
  }
})
藍藍設計m.73404.com.cn )是一家專注而深入的界面設計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網(wǎng)站建設 平面設計服務

日歷

鏈接

個人資料

藍藍設計的小編 http://m.73404.com.cn

存檔

主站蜘蛛池模板: 久久永久影院免费 | 爽好舒服宝贝添奶吻戏 | 99亚洲| 亚洲福利精品电影在线观看 | 波多野结衣中文丝袜字幕 | 538亚洲欧美国产日韩在线精品 | 小浪妇奶真大水多 | 国产精品午夜国产小视频 | 男插女的下面免费视频夜色 | 亚洲小视频在线 | 欧美成人午夜片一一在线观看 | 99视频在线看 | 成人网欧美亚洲影视图片 | 无码中文字幕热热久久 | 污影院| 赤坂丽女医bd无删减在线观看 | 国产亚洲精品第一综合linode | 99热com| 大陆性出航 | 暴露狂婷婷医院暴露tx | 国色天香社区视频在线观看免费完整版 | 欧美free激情野战hd | 日本午夜大片免费观看视频 | 国产全部理论片线观看 | 国内精品 大秀视频 日韩精品 | 范冰冰特黄xx大片 | 国产福利视频一区二区微拍视频 | 青青青视频免费线看 视频 青青青青青国产免费手机看视频 | 国产99re在线观看69热 | 袖珍人与大黑人性视频 | 全是女性放屁角色的手游 | h肉动漫在线视频无修无遮挡 | 男人扒开 | 欧美日韩在线观看一区二区 | www日本视频 | 国产亚洲欧美成人久久片 | 久久99国产综合精品AV蜜桃 | 97蜜桃网| 国产三级精品久久三级国专区 | 亚洲国产一区二区三区a毛片 | 啪啪模拟器|