繁体   English   中英

ESLint: '@material-ui/core/Button' 导入被限制使用

[英]ESLint: '@material-ui/core/Button' import is restricted from being used

我正在编写一个 React 16 应用程序,我正在使用 material-ui 组件。

我像这样导入按钮:

import Button from '@material-ui/core/Button';

就像官方文档中所说的那样: https://material-ui.com/components/buttons/

WebStorm 显示错误消息:

ESLint: '@material-ui/core/Button' 导入被限制使用。 这将加载 package 的 CommonJS 版本。 修复替换为: import { Button } from "@material-ui/core";(no-restricted-imports)

当我将导入更改为:

import {Button} from '@material-ui/core';

消息错误消失。 该代码在这两种情况下都有效。

我想知道为什么我需要更改导入以使错误消失。 为什么我不能只使用 material-ui.com 本身建议的相同代码。

我正在使用的模板使用这个.eslintrc.js

"use strict";

const fs = require("fs");
const path = require("path");

const restrictedPaths = [
  { name: "react-bootstrap" },
  { name: "@material-ui/core" }
].map(pkg =>
  fs
    .readdirSync(path.dirname(require.resolve(`${pkg.name}/package.json`)))
    .map(component => ({
      name: `${pkg.name}/${component}`,
      message: `This loads CommonJS version of the package. To fix replace with: import { ${component} } from "${pkg.name}";`
    }))
);

// TODO: Wait for https://github.com/facebook/create-react-app/pull/7036 to enable rules in react-scripts.

module.exports = {
  extends: "eslint-config-react-app",
  rules: {
    // "no-script-url": "warn",
    "jsx-a11y/anchor-is-valid": "warn",
    "no-restricted-imports": ["error", { paths: [].concat(...restrictedPaths) }]
  }
};

当您指定要导入的文件的完整路径时,您将导入此文件。 当您使用import {Button} from '@material-ui/core'时,您让节点选择要用于此导入的文件。 模块的package.json可以为单个@material-ui/core指定不同的入口点,具体取决于是使用CommonJS还是ES 模块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM