简体   繁体   English

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

[英]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.

相关问题 尝试导入错误:“ImageList”未从“@material-ui/core”导出 - Attempted import error: 'ImageList' is not exported from '@material-ui/core' 如何从“material-ui”导入类型? - How to import types from `material-ui`? 尝试导入错误:“unstable_useId”未从“@material-ui/core/utils”导出(导入为“useId”) - Attempted import error: 'unstable_useId' is not exported from '@material-ui/core/utils' (imported as 'useId') 无法导入@material-ui/core/styles/MuiThemeProvider - Cannot import @material-ui/core/styles/MuiThemeProvider 尝试导入错误:“useControlled”未从“@material-ui/core/utils”导出 - Attempted import error: 'useControlled' is not exported from '@material-ui/core/utils' 尝试导入错误:“fade”未从“@material-ui/core/styles”导出 - Attempted import error: 'fade' is not exported from '@material-ui/core/styles' 尝试导入错误:“alpha”未从“@material-ui/core/styles”导出 - Attempted import error: 'alpha' is not exported from '@material-ui/core/styles' 尝试导入错误:“makeStyles”未从“@material-ui/core/styles”导出 - Attempted import error: 'makeStyles' is not exported from '@material-ui/core/styles' 为什么 Material-UI 按钮的值未传递给 onClick 事件处理程序? - Why value from a Material-UI Button is not being passed to the onClick event handler? 导入错误:在使用 @material-ui/core/styles/ThemeProvider/ 和 @material-ui/core/styles/createMuiTheme/ 时找不到模块 - import error: Module not found on using @material-ui/core/styles/ThemeProvider/ and @material-ui/core/styles/createMuiTheme/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM