英文:
How do I pull in all icons from FontAwesome 5 for a React/Typescript app?
问题
I了解,你想要翻译的部分如下:
我知道有一个Font Awesome的速查表列出了所有的图标,但是使用类名导入对我来说失败了。类似下面的代码会产生一个控制台错误,说它找不到图标。
<FontAwesomeIcon icon={findIconDefinition({ prefix: 'fas', iconName: 'camera' })} />
和
<FontAwesomeIcon icon={['fas','camera']} />
以下是我收到的错误信息。
client.js:1 无法找到图标 {prefix: 'fas', iconName: 'camera'}
console.error @ client.js:1
window.console.error @ next-dev.js:24
log @ index.es.js:278
eval @ index.es.js:352
renderWithHooks @ react-dom.development.js:16305
updateForwardRef @ react-dom.development.js:19233
beginWork @ react-dom.development.js:21636
beginWork$1 @ react-dom.development.js:27426
performUnitOfWork @ react-dom.development.js:26557
workLoopSync @ react-dom.development.js:26466
renderRootSync @ react-dom.development.js:26434
performSyncWorkOnRoot @ react-dom.development.js:26085
flushSyncCallbacks @ react-dom.development.js:12042
flushSync @ react-dom.development.js:26201
scheduleRefresh @ react-dom.development.js:27795
eval @ react-refresh-runtime.development.js:265
performReactRefresh @ react-refresh-runtime.development.js:254
applyUpdate @ helpers.js:123
statusHandler @ helpers.js:140
setStatus @ webpack.js?ts=1684012466139:520
(anonymous) @ webpack.js?ts=1684012466139:691
Promise.then (async)
internalApply @ webpack.js?ts=1684012466139:674
hotApply @ webpack.js?ts=1684012466139:622
eval @ hot-dev-client.js:317
Promise.then (async)
tryApplyUpdates @ hot-dev-client.js:308
handleSuccess @ hot-dev-client.js:84
processMessage @ hot-dev-client.js:217
eval @ hot-dev-client.js:50
eval @ websocket.js:53
handleMessage @ websocket.js:52
目标是制作一个图标选择器。
我尝试使用了这个图标选择器并进行了重新用途 reddit 帖子 但是失败了。
英文:
I know that there is a font awesome cheat sheet that lists all the icons, however pulling in by class name fails for me. Something like the below is producing a console error that it cannot find the icon.
<FontAwesomeIcon icon={findIconDefinition({ prefix: 'fas', iconName: 'camera' })} />
and
<FontAwesomeIcon icon={['fas','camera']} />
The following is the error I received.
client.js:1 Could not find icon {prefix: 'fas', iconName: 'camera'}
console.error @ client.js:1
window.console.error @ next-dev.js:24
log @ index.es.js:278
eval @ index.es.js:352
renderWithHooks @ react-dom.development.js:16305
updateForwardRef @ react-dom.development.js:19233
beginWork @ react-dom.development.js:21636
beginWork$1 @ react-dom.development.js:27426
performUnitOfWork @ react-dom.development.js:26557
workLoopSync @ react-dom.development.js:26466
renderRootSync @ react-dom.development.js:26434
performSyncWorkOnRoot @ react-dom.development.js:26085
flushSyncCallbacks @ react-dom.development.js:12042
flushSync @ react-dom.development.js:26201
scheduleRefresh @ react-dom.development.js:27795
eval @ react-refresh-runtime.development.js:265
performReactRefresh @ react-refresh-runtime.development.js:254
applyUpdate @ helpers.js:123
statusHandler @ helpers.js:140
setStatus @ webpack.js?ts=1684012466139:520
(anonymous) @ webpack.js?ts=1684012466139:691
Promise.then (async)
internalApply @ webpack.js?ts=1684012466139:674
hotApply @ webpack.js?ts=1684012466139:622
eval @ hot-dev-client.js:317
Promise.then (async)
tryApplyUpdates @ hot-dev-client.js:308
handleSuccess @ hot-dev-client.js:84
processMessage @ hot-dev-client.js:217
eval @ hot-dev-client.js:50
eval @ websocket.js:53
handleMessage @ websocket.js:52
The goal is to make an icon picker.
I've tried to use this icon picker and repurpose it reddit post but it failed.
答案1
得分: 2
以下是您要翻译的内容:
我昨晚找到了解决方法,所以想要发布我的解决方案。
主要问题是找不到图标...
错误。Font Awesome 更适合直接导入单个图标,只有在您将它添加到FontAwesome库后,它才会识别像下面这样的文本输入。
<FontAwesomeIcon icon="coffee" />
要修复/测试上述代码的找不到图标...
问题,我必须添加以下代码行。
import { library } from '@fortawesome/fontawesome-svg-core';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';
library.add(faCoffee)
然后没有错误,可以通过文本查找图标。为了解决我最初想要导入所有Font Awesome图标的问题,我将这些代码行更改为以下内容。
import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/free-solid-svg-icons';
library.add(fas)
然后我能够遍历来自速查表的图标类名列表,所有图标都显示出来。
英文:
I figured this out last night so wanted to post my solution.
The main issue was the Could not find icon...
error. Font Awesome is more intended for importing individual icons directly, and it will only recognize a text input, like the following, if you have added it your FontAwesome library.
<FontAwesomeIcon icon="coffee" />
To fix/test the Could not find icon...
for the above code I had to add the following lines of code.
import { library } from '@fortawesome/fontawesome-svg-core';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';
library.add(faCoffee)
No more error then the icon lookup by text worked. To solve my original problem of wanting to import all of the font awesome icons. I changed those lines of code to the following:
import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/free-solid-svg-icons';
library.add(fas)
Then I was able to iterate through the list of icon class names from the cheatsheet and all the icons displayed.
答案2
得分: 0
你首先需要导入这些库:
npm i --save @fortawesome/fontawesome-svg-core
npm install --save @fortawesome/free-solid-svg-icons
npm install --save @fortawesome/react-fontawesome
然后像这样使用它 -
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faEnvelope } from '@fortawesome/free-solid-svg-icons'
return (
<FontAwesomeIcon icon={faEnvelope} />
)
在这种情况下,你必须分别导入每个图标。还有其他导入所有图标的全局替代方法,但这可能会增加您的捆绑包大小。你可以在这里了解更多信息。
英文:
You need to import the libraries first:
npm i --save @fortawesome/fontawesome-svg-core
npm install --save @fortawesome/free-solid-svg-icons
npm install --save @fortawesome/react-fontawesome
And then use it like this -
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faEnvelope } from '@fortawesome/free-solid-svg-icons'
return (
<FontAwesomeIcon icon={faEnvelope} />
)
In this case, you have to import each icon separately.
There are other alternative ways to import all icons globally but that may increase your bundle size. You can read more about that here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论