Javascript VSCode ESLint如何修复解析错误:';导入&x27;和';出口';可能仅出现在顶层

Javascript VSCode ESLint如何修复解析错误:';导入&x27;和';出口';可能仅出现在顶层,javascript,ecmascript-6,visual-studio-code,babeljs,eslint,Javascript,Ecmascript 6,Visual Studio Code,Babeljs,Eslint,注意:代码正在运行,只有ESLint给了我一个我想要修复的错误 执行动态导入时: if (true) import x from 'y'; //Parsing error: 'import' and 'export' may only appear at the top level 我的.eslintrc { "root": true, "extends": "eslint:recommended", "env": { "es6": true, "node": tr

注意:代码正在运行,只有ESLint给了我一个我想要修复的错误

执行动态导入时:

if (true) import x from 'y'; //Parsing error: 'import' and 'export' may only appear at the top level
我的.eslintrc

{
  "root": true,
  "extends": "eslint:recommended",
  "env": {
    "es6": true,
    "node": true,
    "browser": true
  },
  "parserOptions": {
    "ecmaVersion": 10,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "rules": {
    "strict": 0,
    "no-undef": 2,
    "accessor-pairs": 2,
    "comma-dangle": [2, "always-multiline"],
    "consistent-return": 2,
    "dot-notation": 2,
    "eqeqeq": 2,
    "indent": [2, 2, {"SwitchCase": 1}],
    "no-cond-assign": 2,
    "no-constant-condition": 2,
    "no-eval": 2,
    "no-inner-declarations": [0],
    "no-unneeded-ternary": 2,
    "radix": 2,
    "semi": [2, "always"],
    "camelcase": 2,
    "comma-spacing": 2,
    "comma-style": 2,
    "eol-last": 2,
    "linebreak-style": [2, "unix"],
    "new-parens": 2,
    "no-lonely-if": 2,
    "no-multiple-empty-lines": 2,
    "no-nested-ternary": 2,
    "no-spaced-func": 2,
    "no-trailing-spaces": 2,
    "operator-linebreak": 2,
    "quotes": [2, "single"],
    "semi-spacing": 2,
    "space-unary-ops": 2,
    "arrow-parens": 2,
    "arrow-spacing": 2,
    "no-class-assign": 2,
    "no-dupe-class-members": 2,
    "no-var": 2,
    "object-shorthand": 2,
    "prefer-const": 2,
    "prefer-spread": 2,
    "prefer-template": 2
  },
我已经试过了:
…将
ecmaVersion
切换到2018年11月、2019年11月、2020年11月,这给了我错误的号码,或者解析器不再工作
…添加了
解析器:babel eslint
,这使得解析器不再工作

…添加了
allowImportsExportsAnywhere:true
它什么也没做

看起来语法是错误的。试试这个:

if (true) 
  import('y').then((x) => {
     console.log(x);
  });
也许你可以用在这个结构中 在您的组件中

{ your condition ?(<x/>):("")}
{您的情况?():(“”)}
请参见

此版本增加了对ES2020语法的支持,其中包括 用于动态导入和BigInt。这可以使用
ecmaVersion启用:
2020
在您的配置文件中

这似乎建议将其添加到
.eslintrc.json
文件中,然后重新加载vscode:

"parserOptions": {
  "ecmaVersion": 2020,
  "sourceType": "module",
  "ecmaFeatures": {
    "jsx": true
  }
},
或者
“ecmaVersion”:11,
这是相同的东西

但是当我这样做时,我得到一个关于无效的
ecmaVersion的不同错误选项这似乎可以消除该错误

"env": {
    "browser": true,
    "node": true,
    "es2020": true
},

解析选项中的无
ecmaVersion
显然,vscode的eslint版本还不支持
“ecmaVersion”:2020

您是否尝试过require?因此我只能在
then
范围内使用导入?您的解决方案实际上也可以工作,但我想知道为什么eslint会抱怨,因为代码是正确的
"env": {
    "browser": true,
    "node": true,
    "es2020": true
},