[英]ESLint: '@material-ui/core/Button' import is restricted from being used
I'm writing a React 16 application, I'm using material-ui components.我正在编写一个 React 16 应用程序,我正在使用 material-ui 组件。
I import Button like this:我像这样导入按钮:
import Button from '@material-ui/core/Button';
just like it says in the official documentation: https://material-ui.com/components/buttons/就像官方文档中所说的那样: https://material-ui.com/components/buttons/
WebStorm show an error saying: WebStorm 显示错误消息:
ESLint: '@material-ui/core/Button' import is restricted from being used.
ESLint: '@material-ui/core/Button' 导入被限制使用。 This loads CommonJS version of the package.
这将加载 package 的 CommonJS 版本。 To fix replace with: import { Button } from "@material-ui/core";(no-restricted-imports)
修复替换为: import { Button } from "@material-ui/core";(no-restricted-imports)
When I change the import to:当我将导入更改为:
import {Button} from '@material-ui/core';
the message error goes away.消息错误消失。 The code works in both instances.
该代码在这两种情况下都有效。
I want to know why I need to change the import for the error to disappear.我想知道为什么我需要更改导入以使错误消失。 why can't I just use the same code suggested by material-ui.com itself.
为什么我不能只使用 material-ui.com 本身建议的相同代码。
The template I'm using uses this .eslintrc.js
我正在使用的模板使用这个
.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) }]
}
};
When you specify the full path to the file you want to import, then you import this file.当您指定要导入的文件的完整路径时,您将导入此文件。 When you use the
import {Button} from '@material-ui/core'
, you let node choose the file it wants to use for this import.当您使用
import {Button} from '@material-ui/core'
时,您让节点选择要用于此导入的文件。 The package.json
of a module may specify different entry points for a single @material-ui/core
, depending on if working with CommonJS or ES modules .模块的
package.json
可以为单个@material-ui/core
指定不同的入口点,具体取决于是使用CommonJS还是ES 模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.