Vue中使用Vuex获取多个state状态的4种写法

写法①(初级)

computed: {
    count () {
      return this.$store.state.count
    },
    count1 () {
      return this.$store.state.count1
    }
  }

写法②(原理级)

computed: mapState({
    count: state => state.count,
    count1: state => state.count1,
    
    // 官网
    // countAlias是个变量,并把count的值赋给它
    countAlias: 'count',
    // countPlusLoaclState是个方法,可在里面写计算操作并会返回计算后的值,并且可通过this获取data()局部数据的值
    countPlusLoaclState (state) {
      return state.count + this.num
    }
  })

写法③(中级)

computed: mapState([
    'count',
    'count1'
  ])

写法④(高级)

computed: {
    ...mapState([
      'count',
      'count1'
    ])
  }

Vuex核心概念State

Logo

前往低代码交流专区

更多推荐