我有一个关于React的项目,我需要将FontAwesome集成到它。我找到了官方的library并按照自述文件中的说明进行安装
$ npm i --save @fortawesome/fontawesome
$ npm i --save @fortawesome/react-fontawesome
当我尝试在我的代码中使用它时,如下所示:
<FontAwesomeIcon icon='plus'/>
<FontAwesomeIcon icon={['fa', 'coffee']}/>
我在控制台上收到此错误:
Could not find icon {prefix: "fas", iconName: "plus"}
Could not find icon {prefix: "fa", iconName: "coffee"}
我试图将FontAwesome css链接包含到头部,但这没有帮助。我正在使用
npm v4.6.1
; node v8.9.1
; react v16.2
。 最佳答案
您需要将想要使用的任何图标添加到“库”中,以方便引用。
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'));
在这里查看工作代码:
https://codesandbox.io/s/8y251kv448
另外,请阅读以下内容:
https://www.npmjs.com/package/@fortawesome/react-fontawesome#build-a-library-to-reference-icons-throughout-your-app-more-conveniently
关于reactjs - 找不到图标react-fontawesome,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48002619/