vuejs中使用(...)对象扩展运算符报错?

发布于 2022-09-04 09:42:10 字数 1248 浏览 8 评论 0

 import  {getTodos} from '../store/actions'
    import  {mapState, mapActions} from 'vuex'
    export default{
        name: 'VuexDemo',
        data(){
            return {
                msg: 'VuexDemo'
            }
        },
        computed: mapState({
            count: state => state.todo.length
        }),
        methods: {
            ...mapActions(['getTodos'])
        },
        created(){
            this.getTodos()
            console.log(this.count)
        }
    }
    

然后就报错了。。...是非法字符。。

Module build failed: SyntaxError: D:/01workspace/GitHub/mint-demo/src/components/Vuexdemo.vue: Unexpected token (30:8)

  28 |     }),
  29 |     methods: {
> 30 |         ...mapActions(['getTodos'])
     |         ^
  31 |     },
  32 |     created(){
  33 |         this.getTodos()

package.json 部分

"babel-core": "^6.0.0",
"babel-loader": "^6.0.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.0.0",
"babel-preset-stage-3": "^6.17.0",
"babel-runtime": "^6.20.0"

.babelrc

{
  "presets": [
    [
      "es2015",
      "stage-3"
    ]
  ],
  "plugins": [
    "transform-runtime"
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别挽留 2022-09-11 09:42:10

是不是 babel 的配置有问题,只用 stage-3 试试。

爱的十字路口 2022-09-11 09:42:10

对象展开运算符报错时需要安装一个插件: npm install --save-dev babel-plugin-transform-object-rest-spread
在.babelrc文件中配置: {"plugins":["transform-object-rest-spread"]}
转自:https://www.aliyun.com/jiaoch...

何处潇湘 2022-09-11 09:42:10

尝试给方法调用加括号看一下,或许是运算符优先级的问题。

带上头具痛哭 2022-09-11 09:42:10

写上stage-3就行了 ,我之前也不行

等待圉鍢 2022-09-11 09:42:10

你可以参考下ES6的语法,使用扩展运算符的时候,前面必须要有内容的,所以你这样写会报错。

对象扩展运算符使用的场景是methods里面有其他方法,所以你可以这样写:

1、methods里没有其他方法

methods: mapActions(['getTodos']),

2、methods里有其他方法

methods: {
    func:()=>{},
    ...mapActions(['getTodos'])
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文