报错详情:
“Parsing error: ‘import’ and ‘export’ may only appear at the top level”
报错代码
document.querySelector('#btn').onclick = function () {
import(/* webpackChunkName: 'test' */'./print')
.then(({ print }) => {
console.log(print(2, 3));
})
.catch(() => {
console.log('加载失败');
});
};
在该文件中使用懒加载的时候,使用webpack打包,eslint检查出现:'import' and 'export' may only appear at the top level”
为了关闭eslint关于import使用位置的检查,可以在package.json文件中加上如下配置
"eslintConfig": {
"parser": "babel-eslint",// 解析器,默认使用Espree
"parserOptions": {
"sourceType": "module", // 指定来源的类型,"script" (默认) 或 "module"(如果你的代码是 ECMAScript 模块)
"allowImportExportEverywhere": true // 不限制eslint对import使用位置
}
},