简体   繁体   English

找不到模块:无法解析“@material-ui/icons/ContentCut”

[英]Module not found: Can't resolve '@material-ui/icons/ContentCut'

On one of my React project, I'm using Material-UI icons with those info in my package.json在我的一个 React 项目中,我使用 Material-UI 图标和我的 package.json 中的这些信息

"@material-ui/core": "^4.9.10",
"@material-ui/icons": "^4.5.1",

All icons are working fine until I added the icon ContentCut which is availabe at this link :在我添加此链接上可用的图标ContentCut之前,所有图标都可以正常工作: 在此处输入图像描述

this is how I'm importing it:这就是我导入它的方式:

import ContentCutIcon from '@material-ui/icons/ContentCut';

but I get the error:但我得到了错误:

Module not found: Can't resolve '@material-ui/icons/ContentCut'找不到模块:无法解析“@material-ui/icons/ContentCut”

I tried by upgrading the icons package using yarn:我尝试使用纱线升级图标 package:

yarn upgrade @material-ui/core --latest
yarn upgrade @material-ui/icons --latest

but it does not solve the problem.但这并不能解决问题。

I created this codesandbox with the fixed latest version of @material-ui/icons which is today v1.4.9 as described on the npm project page to illustrate the problem (comment the ContentCut import line to see the error).我使用固定的最新版本@material-ui/icons创建了这个代码框,该版本是今天的 v1.4.9,如npm 项目页面上所述,以说明问题(评论ContentCut导入行以查看错误)。

Does anyone solved this specific issue (when the icon is available but the import is not possible even after an npm/yarn upgrade)?有没有人解决了这个特定问题(当图标可用但即使在 npm/yarn 升级后也无法导入)?

Install the material core library:安装材料芯库:

npm install @material-ui/core

You can use the search here to see which icons are part of @material-ui/icons : https://material-ui.com/components/material-icons/ .您可以在此处使用搜索来查看哪些图标是@material-ui/icons的一部分: https://material-ui.com/components/material-icons/ ContentCut is not there. ContentCut 不存在。

If you have the svg for an icon such as from the material.io site, you can create an SvgIcon element by grabbing the path element from the svg:如果您有 svg 图标,例如来自 material.io 站点,您可以通过从 svg 获取path元素来创建SvgIcon元素:

const ContentCutIcon = createSvgIcon(
  <path d="M9.64 7.64c.23-.5.36-1.05.36-1.64 0-2.21-1.79-4-4-4S2 3.79 2 6s1.79 4 4 4c.59 0 1.14-.13 1.64-.36L10 12l-2.36 2.36C7.14 14.13 6.59 14 6 14c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4c0-.59-.13-1.14-.36-1.64L12 14l7 7h3v-1L9.64 7.64zM6 8c-1.1 0-2-.89-2-2s.9-2 2-2 2 .89 2 2-.9 2-2 2zm0 12c-1.1 0-2-.89-2-2s.9-2 2-2 2 .89 2 2-.9 2-2 2zm6-7.5c-.28 0-.5-.22-.5-.5s.22-.5.5-.5.5.22.5.5-.22.5-.5.5zM19 3l-6 6 2 2 7-7V3z" />,
  "ContentCut"
);

Here is a working version of your sandbox leveraging this:这是利用此功能的沙盒的工作版本:

import React from "react";
import MailIcon from "@material-ui/icons/Mail";
import ForwardIcon from "@material-ui/icons/Forward";
import createSvgIcon from "@material-ui/icons/utils/createSvgIcon";

const ContentCutIcon = createSvgIcon(
  <path d="M9.64 7.64c.23-.5.36-1.05.36-1.64 0-2.21-1.79-4-4-4S2 3.79 2 6s1.79 4 4 4c.59 0 1.14-.13 1.64-.36L10 12l-2.36 2.36C7.14 14.13 6.59 14 6 14c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4c0-.59-.13-1.14-.36-1.64L12 14l7 7h3v-1L9.64 7.64zM6 8c-1.1 0-2-.89-2-2s.9-2 2-2 2 .89 2 2-.9 2-2 2zm0 12c-1.1 0-2-.89-2-2s.9-2 2-2 2 .89 2 2-.9 2-2 2zm6-7.5c-.28 0-.5-.22-.5-.5s.22-.5.5-.5.5.22.5.5-.22.5-.5.5zM19 3l-6 6 2 2 7-7V3z" />,
  "ContentCut"
);
export default function App() {
  return (
    <div className="App">
      <h1>This is three working icons</h1>
      <ul>
        <li>
          <MailIcon />
        </li>
        <li>
          <ForwardIcon />
        </li>
        <li>
          <ContentCutIcon />
        </li>
      </ul>
    </div>
  );
}

编辑 ContentCut 图标

npm install @mui/icons-material is the cure for this problems npm install @mui/icons-material是解决这个问题的方法

It means the ContentCut is not available in the node_modules.这意味着 ContentCut 在 node_modules 中不可用。

One way to solve this is to create files manually and save to icons folder inside node_modules.解决此问题的一种方法是手动创建文件并保存到 node_modules 内的图标文件夹。 I already create file for your ContentCut SVG code.我已经为您的 ContentCut SVG 代码创建了文件。

ContentCut.js ContentCut.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _createSvgIcon = _interopRequireDefault(require("./utils/createSvgIcon"));

var _default = (0, _createSvgIcon.default)(_react.default.createElement("path", {
  d: "M9.64 7.64c.23-.5.36-1.05.36-1.64 0-2.21-1.79-4-4-4S2 3.79 2 6s1.79 4 4 4c.59 0 1.14-.13 1.64-.36L10 12l-2.36 2.36C7.14 14.13 6.59 14 6 14c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4c0-.59-.13-1.14-.36-1.64L12 14l7 7h3v-1L9.64 7.64zM6 8c-1.1 0-2-.89-2-2s.9-2 2-2 2 .89 2 2-.9 2-2 2zm0 12c-1.1 0-2-.89-2-2s.9-2 2-2 2 .89 2 2-.9 2-2 2zm6-7.5c-.28 0-.5-.22-.5-.5s.22-.5.5-.5.5.22.5.5-.22.5-.5.5zM19 3l-6 6 2 2 7-7V3z"
}), 'ContentCut');

exports.default = _default;

ContentCut.d.ts ContentCut.d.ts

export { default } from '@material-ui/core/SvgIcon';

add to this path \node_modules@material-ui\icons添加到此路径\node_modules@material-ui\icons

then this import to your component然后这个导入到你的组件

import ContentCutIcon from '@material-ui/icons/ContentCut';

<ContentCutIcon />

i had the same issue in react functionnal component, seems link to instance.我在反应功能组件中遇到了同样的问题,似乎链接到实例。 I have this working but if audio is inside SongButton, it doesn't work anymore我有这个工作,但如果音频在 SongButton 内,它就不再工作了

const audio = new Audio("../song.mp3")
const SongButton = () =>{

const [isAudio, setIsAudio] = React.useState(false)

const onClick = () => {
    if(isAudio){
        audio.pause();
        //audio.currentTime = 0;
    }else{
        audio.play();
    }
    setIsAudio(!isAudio);
  }

  if(isAudio){
      return (
          <div className="SongButtonDiv">
               <PauseCircleOutlineIcon onClick={onClick} fontSize="large" />
          </div>
      )
  }else{
      return (
          <div className="SongButtonDiv">
               <PlayCircleOutlineIcon onClick={onClick} fontSize="large"/>
          </div>
       )
   }
}
export default SongButton

Solution that works for me:适合我的解决方案:

    npm i @material-ui/icons

If you are developing with react run this in the terminal如果您正在使用 react 进行开发,请在终端中运行
yarn add @material-ui/core @material-ui/icons

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

相关问题 材质 UI 的问题 - 未找到模块:无法解析“@material-ui/icons/Add” - A problem with Material UI - Module not found: Can't resolve '@material-ui/icons/Add' ./node_modules/material-ui-icons/AccessAlarm.js 找不到模块:无法解析“material-ui/SvgIcon” - ./node_modules/material-ui-icons/AccessAlarm.js Module not found: Can't resolve 'material-ui/SvgIcon' 找不到模块:无法解析“material-ui/AutoComplete” - Module not found: Can't resolve 'material-ui/AutoComplete' 找不到模块:无法解析“@material-ui/styles”(反应) - Module not found: Can't resolve '@material-ui/styles' (React) 找不到模块:无法解析“ material-ui / FontIcon” - Module not found: Can't resolve 'material-ui/FontIcon' "未找到模块:无法解析“material-ui\/styles\/colors”" - Module not found: Can't resolve 'material-ui/styles/colors' 找不到模块:无法解析“@material-ui/core/AppBar” - Module not found: Can't resolve '@material-ui/core/AppBar' 找不到模块:无法解析“@material-ui/core/Container” - Module not found: Can't resolve '@material-ui/core/Container' 找不到模块:无法解析“@material-ui/core/Breadcrumbs” - Module not found: Can't resolve '@material-ui/core/Breadcrumbs' Material-ui:找不到模块:无法解析“material-ui/Toolbar” - Material-ui: Module not found: Can't resolve 'material-ui/Toolbar'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM