React 中的 Font Awesome 图标
Font Awesome icon in React
使用字符串作为图标参数的值时,我无法使 Font Awesome 图标正常工作。
这项工作:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
<FontAwesomeIcon icon={faCoffee} />
然而这不是:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
<FontAwesomeIcon icon="coffee" />
根据这个 documentation 它应该。
但我是 React 的新手,所以可能会遗漏一些东西。
使用javascript。使用 react cli 创建的项目。
Package.json:
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.34",
"@fortawesome/free-brands-svg-icons": "^5.15.2",
"@fortawesome/free-regular-svg-icons": "^5.15.2",
"@fortawesome/free-solid-svg-icons": "^5.15.2",
"@fortawesome/react-fontawesome": "^0.1.14",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.3",
"@testing-library/user-event": "^12.6.2",
"font-awesome": "^4.7.0",
"node-sass": "^4.14.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-metismenu": "^1.4.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"rsuite": "^4.8.8",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
我所说的“不工作”是指图标在页面上不可见并且控制台出现错误:“找不到图标 {prefix: “fas”, iconName: “coffee”}”
您需要像这样导入 library
:import { library } from '@fortawesome/fontawesome-svg-core'
然后添加您要使用的图标。
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
library.add(faCoffee /*, faCheckSquare, etc*/)
在此之后,您应该可以将图标用作“咖啡”了。如果您没有将图标添加到库中,它们将不会包含在捆绑包中,因此会出现错误。
使用字符串作为图标参数的值时,我无法使 Font Awesome 图标正常工作。
这项工作:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
<FontAwesomeIcon icon={faCoffee} />
然而这不是:
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
<FontAwesomeIcon icon="coffee" />
根据这个 documentation 它应该。 但我是 React 的新手,所以可能会遗漏一些东西。
使用javascript。使用 react cli 创建的项目。
Package.json:
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.34",
"@fortawesome/free-brands-svg-icons": "^5.15.2",
"@fortawesome/free-regular-svg-icons": "^5.15.2",
"@fortawesome/free-solid-svg-icons": "^5.15.2",
"@fortawesome/react-fontawesome": "^0.1.14",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.3",
"@testing-library/user-event": "^12.6.2",
"font-awesome": "^4.7.0",
"node-sass": "^4.14.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-metismenu": "^1.4.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"rsuite": "^4.8.8",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
我所说的“不工作”是指图标在页面上不可见并且控制台出现错误:“找不到图标 {prefix: “fas”, iconName: “coffee”}”
您需要像这样导入 library
:import { library } from '@fortawesome/fontawesome-svg-core'
然后添加您要使用的图标。
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
library.add(faCoffee /*, faCheckSquare, etc*/)
在此之后,您应该可以将图标用作“咖啡”了。如果您没有将图标添加到库中,它们将不会包含在捆绑包中,因此会出现错误。