Vue2.3.3中使用对象展开运算符出错

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 18:00   1923   0

在vue应用中使用vuex中的...mapGetters时得到如下错误:

<script>
  import VLink from '../components/VLink.vue'
  import store from '../vuex/store';
  import { mapGetters } from 'vuex';

  export default {
    store,
    components: {
      VLink
    },
    computed: {
      // 使用对象展开运算符将 getter 混入 computed 对象中
      ...mapGetters([
        'doneTodos',
        'totalDone',
        // ...
      ]),
      count() {
        return this.$store.state.count;
      },
    },
    methods: {
      increment() {
        this.$store.commit('increment');
      },
      decrement() {
        this.$store.commit('decrement');
      }
    }
  }
</script>

错误:

... unexpected token

解决方法:

这里的 ...是es6的对象扩容运算符,目前bable暂不支持,需要引入新的包来解决

1. npm install babel-plugin-transform-object-rest-spread --save-dev
2. 到根目录修改.babelrc文件
// .babelrc 文件
  "plugins": ["transform-object-rest-spread"] //新增这一行

转载于:https://www.cnblogs.com/juliazhang/p/11340366.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP