Vue2.3.3中使用對象展開運算符出錯


在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"] //新增這一行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM