我试图通过 webpack 仅捆绑所需的 Font Awesome 5 图标,但这些图标不会在 DOM 中被替换。
我已经从文档中添加了所有必需的包:
yarn add -D @fortawesome/fontawesome yarn add -D @fortawesome/fontawesome-pro-solid yarn add -D @fortawesome/fontawesome-pro-regular yarn add -D @fortawesome/fontawesome-free-brands
包含以下自定义 JS:
"use strict"; import fontawesome from '@fortawesome/fontawesome'; import faCheck from '@fortawesome/fontawesome-pro-regular/faCheck'; fontawesome.icon(faCheck); function iconsDoneRendering () { console.log('Icons have rendered'); // No output in console } fontawesome.dom.i2svg({ callback: iconsDoneRendering, })
HTML 模板:
<head> <link rel="stylesheet" href="/css/app.css?v2.1.4"> <!-- contains fa-svg-with-js.css --> </head> <body> <ul class="fa-ul"> <li><span class="fa-li"><i class="far fa-phone"></i></span>List item 1</li> <li><span class="fa-li"><i class="far fa-check"></i></span>List item 2</li> </ul> <script src="/js/app.js?v2.1.4"></script> </body>
svg 路径在捆绑的 JS 文件中,但我不知道需要调用哪个方法。
下面的 JS 代码解决了这个问题(从 v5.0.2 开始):
"use strict";
import fontawesome from '@fortawesome/fontawesome';
import faCheck from '@fortawesome/fontawesome-pro-regular/faCheck';
import faPhone from '@fortawesome/fontawesome-pro-regular/faPhone';
fontawesome.library.add(faCheck,faPhone);