如何在React JS中添加Fontawesome图标?
我正在尝试使用下面的代码在我的React应用中添加Fontawesome图标(请参阅 codesandbox ),但图标不会显示。
./src/app.js
import "./styles.css";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<FontAwesomeIcon icon="fa-solid fa-home"></FontAwesomeIcon>
<div>hello</div>
<FontAwesomeIcon icon="fas fa-edit" />
<div>hello</div>
<span>
<i class="fas fa-edit"></i>
</span>
</div>
);
}
./public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<script
defer
src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"
></script>
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Package.json的一部分
"dependencies": {
"@fortawesome/fontawesome-svg-core": "6.1.1",
"@fortawesome/free-solid-svg-icons": "6.1.1",
"@fortawesome/react-fontawesome": "0.1.18",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-scripts": "4.0.0"
},
I'm trying to add a fontawesome icon in my react app using the code below (see the codesandbox) but the icons won't show.
./src/App.js
import "./styles.css";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<FontAwesomeIcon icon="fa-solid fa-home"></FontAwesomeIcon>
<div>hello</div>
<FontAwesomeIcon icon="fas fa-edit" />
<div>hello</div>
<span>
<i class="fas fa-edit"></i>
</span>
</div>
);
}
./public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<script
defer
src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"
></script>
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
part of package.json
"dependencies": {
"@fortawesome/fontawesome-svg-core": "6.1.1",
"@fortawesome/free-solid-svg-icons": "6.1.1",
"@fortawesome/react-fontawesome": "0.1.18",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-scripts": "4.0.0"
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会导入图标:
You might import the icon like: