ESLINT错误:'默认值'限制被用作出口名称

发布于 2025-01-17 22:48:24 字数 1520 浏览 0 评论 0原文

我在 index.js 文件中收到此 eslint 错误:

'default' 被限制用作导出名称 no-restricted-exports

page / index.js

export { default } from './test';

page / test.jsx

import React from 'react';

const Test = () => {
  return <div>Test</div>;
};
export default Test;

routes.js

import React from 'react';
import { Switch, Route } from 'react-router-dom';

const testPage = React.lazy(() => import('./page'));

function Routes() {
  return (
    <Switch>
      <Route path="/" exact component={testPage} />
    </Switch>
  );
}
export default Routes;

.eslintrc.json

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": ["plugin:react/recommended", "airbnb"],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never"
      }
    ],
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [".js", ".jsx"]
      }
    ]
  },
  "settings": {
    "import/extensions": [".js", ".jsx"],
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx"]
      }
    }
  }
}

在不禁用它的情况下解决它的正确方法是什么?

I am getting this eslint error in index.js file:

'default' is restricted from being used as an exported name no-restricted-exports

page / index.js

export { default } from './test';

page / test.jsx

import React from 'react';

const Test = () => {
  return <div>Test</div>;
};
export default Test;

routes.js

import React from 'react';
import { Switch, Route } from 'react-router-dom';

const testPage = React.lazy(() => import('./page'));

function Routes() {
  return (
    <Switch>
      <Route path="/" exact component={testPage} />
    </Switch>
  );
}
export default Routes;

.eslintrc.json

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": ["plugin:react/recommended", "airbnb"],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never"
      }
    ],
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [".js", ".jsx"]
      }
    ]
  },
  "settings": {
    "import/extensions": [".js", ".jsx"],
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx"]
      }
    }
  }
}

What would be the proper way to resolve it, without disabling that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅唱々樱花落 2025-01-24 22:48:24
export { default as Test } from './test';

如果使用以上是一个,则您可以执行命名导入而不是这样的默认导入,

import { Test } from './pathToTestFolder'

这不是错误的,但是仍在使用默认导入您默认情况下导出test。您可以执行此

page/index.js

import Test from './test'

export default Test

现在可以使用默认导入

routes.js

import Test from './pathToTestFolder'

或根据您的路由文件

const testPage = React.lazy(() => import('./page'));
export { default as Test } from './test';

if above one is used then you've to do named import instead of default import like this

import { Test } from './pathToTestFolder'

this is not wrong, but to still using the default import you've to export test by default. You can do this

page/index.js

import Test from './test'

export default Test

now you can use default import like this
routes.js

import Test from './pathToTestFolder'

or according to your route file

const testPage = React.lazy(() => import('./page'));
痴情 2025-01-24 22:48:24

您不能导出默认,因为它是保留的关键字。
您可以用:

export { default as Test } from './test';

You cannot export default because it's a reserved keyword.
You could replace the line with :

export { default as Test } from './test';
暖阳 2025-01-24 22:48:24

nameddefault 导出与 ESLint 规则冲突的情况下,您可以使用 * 导出方法,如下所示:

export * from './test';

In situations where named and default export is conflicting with ESLint rules, you can use * export approach as follows:

export * from './test';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文