简体   繁体   English

如何导入材质 ui 图标?我在使用 Material ui 图标时遇到了一些问题

[英]how to import material ui icons?i met some problems using Material ui icons

I was using material UI with react in my project,and i have some troubles when it come to import the material icons,my code is copied from the material UI (version:"material-ui": "^1.0.0-beta.41", "material-ui-icons": "^1.0.0-beta.36",) docs,just like this:我在我的项目中使用了带有react的材质UI,在导入材质图标时遇到了一些麻烦,我的代码是从材质UI复制的(版本:“material-ui”:“^1.0.0-beta. 41", "material-ui-icons": "^1.0.0-beta.36",) 文档,就像这样:

import SkipPreviousIcon from '@material-ui/icons/SkipPrevious';
import PlayArrowIcon from '@material-ui/icons/PlayArrow';
import SkipNextIcon from '@material-ui/icons/SkipNext';

and also i have run npm install material-icons.而且我已经运行 npm 安装材料图标。 the error in my chrome console is:我的 chrome 控制台中的错误是:

./src/index/musicCard.js Module not found: Can't resolve '@material-ui/icons/PlayArrow' in 'C:\Users\wenji\Desktop\myblog\src\index' and I tried this one: ./src/index/musicCard.js Module not found: Can't resolve '@material-ui/icons/PlayArrow' in 'C:\Users\wenji\Desktop\myblog\src\index' 我试过这个:

import SkipPreviousIcon from 'material-ui/icons/SkipPrevious';

and this one:和这个:

import SkipPreviousIcon from '@material-ui-icons/SkipPrevious';

but dose not make any difference,so can anyone help me?但没有任何区别,所以有人可以帮助我吗?

Icons are not part of material-ui/core so it must be install using two commands.图标不是 material-ui/core 的一部分,因此必须使用两个命令安装它。

If you are using npm如果你使用 npm

npm install @material-ui/core
npm install @material-ui/icons

If you are using yarn如果你使用纱线

yarn add @material-ui/core
yarn add @material-ui/icons

Solved, the icons module should be added to dependencies.解决了,应该将图标模块添加到依赖项中。 use npm使用 npm

npm install @material-ui/icons 

or use yarn或使用纱线

yarn add @material-ui/icons 

Change the import path from @mui/icons-material/ to @material-ui/icons/将导入路径从@mui/icons-material/改为@material-ui/icons/

This is not a 100% working solution, as there have been icons I have yet to be able to import (eg ConnectWithoutContact )这不是一个 100% 有效的解决方案,因为我还没有能够导入一些图标(例如ConnectWithoutContact

Regardless, this change has saved me several times so here is an example:不管怎样,这个改变已经救了我好几次了,所以这里是一个例子:

// Initial
import PermContactCalendarIcon from '@mui/icons-material/PermContactCalendar';

// Fixed
import PermContactCalendarIcon from '@material-ui/icons/PermContactCalendar';

I just solved a strange, ( but not so strange after i found out why) issue我刚刚解决了一个奇怪的问题(但在我发现原因后就不那么奇怪了)

on mac it worked but when i deploy to linux it failed and could not find the icon在 mac 上它可以工作,但是当我部署到 linux 时它失败并且找不到图标

this was because on mac it is not case sensitive and linux is这是因为在 mac 上它不区分大小写,而 linux 是

so所以

import DeleteForEver from '@material-ui/icons/DeleteForEver'

works on mac but fails on linux在 mac 上工作但在 linux 上失败

the file is actually named like "DeleteForever"该文件实际上被命名为“DeleteForever”

so correct way to import is所以正确的导入方法是

import DeleteForever from '@material-ui/icons/DeleteForever'

For latest versions you need对于您需要的最新版本

npm install @mui/icons-material

Since Material-UI is now MUI由于Material-UI 现在是 MUI

material ui doesn't provide icons from "@material-ui/icons" anymore. material ui 不再提供来自“@material-ui/icons”的图标 Instead you need to import icons from "@mui/icons-material" .相反,您需要从"@mui/icons-material"导入图标。 So, if you are using the latest version and running your project with npm , you need to execute the following command-因此,如果您使用最新版本并使用npm运行项目,则需要执行以下命令 -

npm install @mui/icons-material

If you use yarn then run following-如果你使用纱线然后运行以下 -

yarn add @material-ui/icons

Now you are all set to use your material icon ExampleMaterialIcon like this-现在你已经准备好像这样使用你的材质图标ExampleMaterialIcon -

import ExampleMaterialIcon from '@mui/icons-material/ExampleMaterialIcon';

Dependencies:依赖项:

"@material-ui/core": "^4.12.4",
"@material-ui/icons": "4.11.3",

Example:例子:

import FavoriteIcon from "@material-ui/icons/Favorite";
import ShareIcon from "@material-ui/icons/Share";

 <FavoriteIcon
            fontSize="large"
            style={{ fill: "red", stroke: "red" }}
  />

 <ShareIcon fontSize="large" />

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

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