最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決VUEX的mapState/...mapState等取值問題

 更新時(shí)間:2020年07月24日 09:55:26   作者:rainbow8300  
這篇文章主要介紹了解決VUEX的mapState/...mapState等取值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

有木有發(fā)現(xiàn)你頁面的this.$store,一般都正常,但是總有那么幾次,成為你頁面報(bào)錯(cuò)的罪魁禍?zhǔn)?!,那其?shí)除了和vue的生命周期有關(guān)以外,還跟store取值的方式有關(guān),下面就說一下新大陸之——mapState mapMutations mapGetters的使用

簡單說一下我對(duì)mapState的理解,字面意思就是把store中state 的值遍歷出來,任你取用,就不用寫this.$store.getters.getState.openId等這么長的取值了,同理,mapMutations mapGetters也是把store中對(duì)應(yīng)的mutations getters方法遍歷出來

下面上代碼,看一下具體怎么操作

store.js代碼

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
 state: {
 showLoading0: true,
 showLoading1: true,
 showLoading2: true,
 showLoading3: true,
 // 頭大不復(fù)制了......
 showLoading9: true,
 },
 // getters 適用于獲取值,不會(huì)改變state原值
 getters: {
 getStore (state) {
  return state
 }
 },
 // 修改state
 mutations: {
 updateLoading (state, showLoading) {
  state.showLoading = showLoading
 }
 },
 actions: {

 }
})

原來vue中的代碼

<script>
export default {
 data() {
 return {
  showLoading0: this.$store.getters.getStore.showLoading0,
  showLoading1: this.$store.getters.getStore.showLoading1,
  showLoading2: this.$store.getters.getStore.showLoading2,
  showLoading3: this.$store.getters.getStore.showLoading3,
  // 頭大不復(fù)制了......
  showLoading9: this.$store.getters.getStore.showLoading9
 }
 },
 methods: {
 updateLoading() {
  this.$store.commit('updateLoading', false)
 }
 }
}
</script>

弱弱的問問看官,你腳的這個(gè)this.$store煩不煩,這要是有十個(gè)值,甚至要看到整整齊齊的十個(gè)this.$store…唉呀,真可怕,這時(shí)候mapState的用途就來了,看代碼:

<script>
// 用之前要先引入vuex中的mapXXX方法
import { mapState } from 'vuex'
export default {
 data() {
 return {
  showLoading0: (state) => state.showLoading0
  showLoading1: (state) => state.showLoading1
  showLoading2: (state) => state.showLoading2
  showLoading3: (state) => state.showLoading3
  // 頭大不復(fù)制了......
  showLoading9: (state) => state.showLoading9
 }
 }
}
</script>

聽說你還不滿意?那看看下面的簡寫吧,別被自己曾經(jīng)的方法蠢哭哦~

// 用之前要先引入vuex中的mapXXX方法
import { mapState } from 'vuex'
export default {
 //data() {
 // return {
 // }
 //}
 // 對(duì)你沒有看錯(cuò)哦,不用data接收了,直接用computed,使用和data里一模一樣哦~
 // <h1 v-if="showLoading0">{{showLoading1}}</h1>
 // console.log(this.showLoading9)
 computed: {
 ...mapState([
  'showLoading0','showLoading1',....,'showLoading9'
 ])
 }
}

mapMutations mapGetters的使用也是同理

// 用之前要先引入vuex中的mapXXX方法
import { mapState } from 'vuex'
export default {
 // 使用 this.getStore()
 computed: {
 ...mapGetters([
  'getStore'
 ])
 },
 // 使用this.updateLoading() 
 // 就相當(dāng)于this.$store.commit('updateLoading')
 methods: {
 ...mapMutations([
   'updateLoading'
  ]),
 }
}

其實(shí)最好采用computed的方式取值,這樣會(huì)避免很多問題的發(fā)生,特別是數(shù)據(jù)更新不及時(shí)~

補(bǔ)充知識(shí):vuex的mapState方法來獲取vuex的state對(duì)象中屬性

有兩種寫法

1.首先在組件中引入vuex的mapState方法:

import { mapState } from 'vuex'

然后在computed中這樣寫:

computed:{
  ...mapState({
    save:state => state.save//使用ES6的箭頭函數(shù)來給count賦值
  })
}

2.需要先在組件中引入vuex的mapState方法:

import { mapState } from 'vuex'

然后在computed中這樣寫:

computed:

 ...mapState([' save'])
}

以上這篇解決VUEX的mapState/...mapState等取值問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

丘北县| 宁陵县| 文昌市| 贵溪市| 朝阳市| 翁源县| 武清区| 陆良县| 沙湾县| 永安市| 昌江| 海门市| 墨江| 华蓥市| 庄河市| 玉田县| 砀山县| 达尔| 玛多县| 开化县| 苏州市| 波密县| 铜陵市| 南城县| 武隆县| 汨罗市| 镇安县| 华容县| 清新县| 扶绥县| 辰溪县| 泾阳县| 苍山县| 白沙| 景德镇市| 买车| 东乡| 平阴县| 夏河县| 南丰县| 涟源市|