为了账号安全,请及时绑定邮箱和手机立即绑定

vuex当中state通过mapState映射到computed后,原来的计算属性要怎么写呢?

vuex当中state通过mapState映射到computed后,原来的计算属性要怎么写呢?

MM们 2018-09-01 09:22:30
import {mapState} from 'vuex'export default {  data(){      return {          oldData: 0      }  }  computed: mapState({    count: state => state.count,    newData(){       return this.oldData + 1;    }  })}
查看完整描述

2 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

import { mapState } from 'vuex'


export default {

  data () {

    return {

      localCount: 1

    }

  },

  // mapState 辅助函数帮助我们生成计算属

  computed: mapState({

    // 箭头函数可使代码更简练

    count: state => state.count,

    // 传字符串参数 'count' 等同于 'state => state.count'

    countAlias: 'count',

    // 为了能使用 'this'获取局部状态,必须使用常规函数

    countPlusLocalState (state) {

      return state.count + this.localCount

    },

    // 常规 computed, 没有使用 store的状态

    localCountAlias () {

      return this.localCount

    }

  })

}


查看完整回答
反对 回复 2018-09-09
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

import {mapState} from 'vuex'


export default {

  data() {

    return {        //你这里少写了

        oldData: 0

    }

  }

  computed: {

    ...mapState(["count"]),

    newData(){

       return this.oldData + 1;

    }

  }

}


查看完整回答
反对 回复 2018-09-09
  • 2 回答
  • 0 关注
  • 8311 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信