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',
    }
  }

 

posted @ 2020-10-20 17:21  Silvia1108  阅读(3438)  评论(0)    收藏  举报