简体   繁体   English

找不到图标 react-fontawesome

[英]Cound not find icon react-fontawesome

I have a project on React and I need to integrate FontAwesome to it.我有一个关于 React 的项目,我需要将 FontAwesome 集成到其中。 I found official library and installed it as instructed in readme我找到了官方并按照自述文件中的说明安装了它

$ npm i --save @fortawesome/fontawesome
$ npm i --save @fortawesome/react-fontawesome

When I try to use it in my code like this:当我尝试在我的代码中使用它时:

<FontAwesomeIcon icon='plus'/>
<FontAwesomeIcon icon={['fa', 'coffee']}/>

I am getting this error on the console:我在控制台上收到此错误:

Could not find icon {prefix: "fas", iconName: "plus"}
Could not find icon {prefix: "fa", iconName: "coffee"}

I tried to include FontAwesome css link to head but it didn't help.我试图将 FontAwesome css 链接包含到标题中,但没有帮助。 I am using npm v4.6.1 ;我正在使用npm v4.6.1 node v8.9.1 ; node v8.9.1 react v16.2 . react v16.2

You need to add any icons you wish to use, to a "library" for easy reference.您需要将您希望使用的任何图标添加到“库”中以便于参考。

import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import fontawesome from '@fortawesome/fontawesome'
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
import { faCheckSquare, faCoffee } from '@fortawesome/fontawesome-free-solid'

const styles = {
  fontFamily: 'sans-serif',
  textAlign: 'center',
};

fontawesome.library.add(faCheckSquare, faCoffee);

const App = () => (
  <div style={styles}>
    <FontAwesomeIcon icon="check-square" /><br /><br />
    <FontAwesomeIcon icon="coffee" />
  </div>
);

render(<App />, document.getElementById('root'));

Check out working code here: https://codesandbox.io/s/8y251kv448在此处查看工作代码: https : //codesandbox.io/s/8y251kv448

Also, read this: https://www.npmjs.com/package/@fortawesome/react-fontawesome#build-a-library-to-reference-icons-throughout-your-app-more-conveniently另外,请阅读: https : //www.npmjs.com/package/@fortawesome/react-fontawesome#build-a-library-to-reference-icons-throughout-your-app-more-conveniently

Just in case there are other idiots out there like me, make sure that you use the right name in referencing the icons.以防万一还有其他像我这样的白痴,请确保在引用图标时使用正确的名称。

I had我有

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faUser } from "@fortawesome/free-solid-svg-icons";

library.add(faUser);

and

render() {
  return (        
    <FontAwesomeIcon icon="faUser" />
  );
}

when, in fact, the actual icon name is just "user", not "faUser":实际上,实际的图标名称只是“user”,而不是“faUser”:

render() {
  return (        
    <FontAwesomeIcon icon="user" />
  );
}

You can use FontAwesome icons without library like this:您可以使用没有库的 FontAwesome 图标,如下所示:

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCoffee } from '@fortawesome/free-solid-svg-icons'

<FontAwesomeIcon icon={faCoffee} />

I've installed all the necessary packages as react-fontawesome says:我已经安装了所有必要的软件包,如react-fontawesome所说:

$ npm i --save @fortawesome/fontawesome-svg-core
$ npm i --save @fortawesome/free-solid-svg-icons
$ npm i --save @fortawesome/react-fontawesome

And if you find yourself not seeing your icon when trying to display faTrashAlt (or similarly named icon), not only do you have to remove the 'fa' when actually using your icon but also you have to convert the icon name from cameCase to "lisp-case".如果您在尝试显示faTrashAlt (或类似名称的图标)时发现自己没有看到您的图标,那么您不仅需要在实际使用图标时删除“fa”,还必须将图标名称从 cameCase 转换为“口齿不清”。

So, after loading the alternative trash can icon this way:因此,在以这种方式加载替代垃圾桶图标后:

fontawesome.library.add(faTrashAlt);

It's then used that way:然后以这种方式使用:

<FontAwesomeIcon icon="trash-alt" />

Just so you don't waste 20 precious minutes of your time.只是为了让您不要浪费宝贵的 20 分钟时间。

Just because there is more than one way to get a fontawesome icon into a website, I'll give an example with react and fontawesome and explicit import.仅仅因为有不止一种方法可以将 fontawesome 图标放入网站,我将举一个带有 react 和 fontawesome 以及显式导入的示例。 Explicit import is frugal.显式导入是节俭的。 Only the exact icon from the fontawesome icon set is imported and the FontAwesomeIcon component icon attribute is set to the imported object instead of the name.仅导入 fontawesome 图标集中的确切图标,并将 FontAwesomeIcon 组件图标属性设置为导入的对象而不是名称。

Example:示例:

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';
import { faHistory } from '@fortawesome/pro-regular-svg-icons';

function exampleReactComponent() {
   return (
     <div>
       <p><FontAwesomeIcon icon={faCoffee}/></p>
       <p><FontAwesomeIcon icon={faHistory}/></p>
     </div>
   );
}

I don't disagree with setting the icon attribute of the FontAwesomeIcon by name it's just that I encountered console errors about finding icons by name and the resolution I discovered is to reference the object.我不反对按名称设置 FontAwesomeIcon 的图标属性,只是我遇到了有关按名称查找图标的控制台错误,而我发现的解决方案是引用该对象。 Note the icon={} notation because it is different than using a quoted string of the icon name.请注意 icon={} 表示法,因为它与使用带引号的图标名称字符串不同。

Explicit Import... react-fontawesome npm package github link显式导入... react-fontawesome npm 包 github 链接

Installing with npm... installing fontawesome with npm link使用 npm 安装...使用 npm 链接安装 fontawesome

To pass the icon path using an array like this使用这样的数组传递图标路径

icon={['fa', 'coffee']} icon={['fa', '咖啡']}

You'll need to import all references of the library in a file and import this file in the index of your React app.您需要将库的所有引用导入到一个文件中,并将此文件导入到您的 React 应用程序的索引中。

Example, create the file named fonts.js示例,创建名为 fonts.js 的文件

import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/pro-solid-svg-icons';
import { far } from '@fortawesome/pro-regular-svg-icons';
import { fal } from '@fortawesome/pro-light-svg-icons';
import { fad } from '@fortawesome/pro-duotone-svg-icons';
import { fab } from '@fortawesome/free-brands-svg-icons';

library.add(fab);
library.add(fas);
library.add(far);
library.add(fal);
library.add(fad);

Now in the index.js of your app import the file that was created,现在在您的应用程序的 index.js 中导入创建的文件,

import '../configs/fonts.js'

This path is just an example, be sure that you are putting the right one.这条路径只是一个例子,请确保您放置的是正确的路径。

For free solid only仅限免费固体

// Fontawesome
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
import { library } from '@fortawesome/fontawesome-svg-core'
import { fas } from '@fortawesome/free-solid-svg-icons'
library.add(fas);
...

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

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