Vue无法识别ES2020新特性??

520 阅读1分钟

今天从guthub拉一个项目,运行的时候遇到一个bug

image.png

刚开始尝试了很多方法都不行,后来才清楚,只是vue模板不支持可选链(??) ,js是支持的。

在网上看了别人的解决办法:他们把源码的 "??"或者 "?."给改成vue模板能识别的语法。 但是我的源码一大推 ES2020新语法。所以我只有自己寻找解决办法了。

解决:安装一个babel

vue-template-babel-compiler:Enable Optional Chaining(?.), Nullish Coalescing(??) and many new ES syntax for Vue.js SFC based on Babel

1.

npm install vue-template-babel-compiler --save-dev

2. vue.config.js文件配置如下

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => {
        options.compiler = require('vue-template-babel-compiler')
        return options
      })
  }
}

github链接:github.com/JuniorTour/…