vue-cli项目中出现“unexpected token import ”时的解决办法


开启eslint语法校验的时候,运行项目会出现“unexpected token import ”这个问题,经过查找后发现是es6语法问题,导致不可以使用import,下面来具体分析一下这个错误;

ES6语法的模块导入导出(import/export)功能,我们在使用它的时候,可能会报错:

 

 

可以通过以下方式解决

1.安装@babel/plugin-syntax-dynamic-import --save-dev

1 yarn add eslint-plugin-vue --save-dev

2.配置babel.config.js文件

 

module.exports = {
  "presets": [
    "@vue/app"
  ],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      },
	  "syntax-dynamic-import"
    ]
  ]
}

3.安装 eslint-plugin-vue

1 yarn add eslint-plugin-vue --save-dev

4.配置eslintrc.js文件

module.exports = {
    root: true,
    parserOptions: {
      sourceType: 'module'
    },
    env: {
      browser: true,
      node: true,
      es6: true,
    },
    parser: "vue-eslint-parser",
    
    rules: {
      'no-console': 'off',
    }
  }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM