使用字体很棒6图标()API(软件包管理器)
Font Awesome 6 提供了 Javascript API: https://fontawesome.com/v6/docs/apis/javascript/methods
然而,他们关于如何加载图标的文档尚不清楚。我知道它应该自动用内联的 替换所有
标签(但这对我不起作用,我知道 FA6发布不稳定,我现在只是假设这是一个错误)。
不管怎样,他们有一个名为“icon”的方法,其唯一的描述是:
将图标渲染为 SVG。
https://fontawesome.com/ v6/docs/apis/javascript/methods#icon-icondefinition-params][2]
调用该方法只会返回一个带有 SVG 的对象 信息。根据他们对其基本用法的描述(上面的链接),它说我只需要调用 icon() 并传入对 Font Awesome 图标的引用。
import { icon } from '@fortawesome/fontawesome-svg-core'
import { faPlus } from '@fortawesome/free-solid-svg-icons'
const faPlusIcon = icon(faPlus)
这实际上并不进行任何渲染。我可以调用 icon(...).html 并将该 html 附加到文档中(并且这有效),但这似乎不是使用 API 的正确方法(只是没有明确说明)。
Font Awesome 6 provides a Javascript API:
https://fontawesome.com/v6/docs/apis/javascript/methods
However, their documentation on how an icon should be loaded is unclear. I know it is supposed to automatically replace all the <i>
tags with inlined <svg>
's (but that isn't working for me, I know FA6 had a shaky launch and I'm just assuming it's a bug right now).
Anyway, they have a method called "icon" whose only description is:
Renders an icon as SVG.
https://fontawesome.com/v6/docs/apis/javascript/methods#icon-icondefinition-params][2]
Calling the method simply returns me an object with the SVG information. Based on their description of its basic usage (link above), it says I just need to call icon() and pass in a reference to a Font Awesome icon.
import { icon } from '@fortawesome/fontawesome-svg-core'
import { faPlus } from '@fortawesome/free-solid-svg-icons'
const faPlusIcon = icon(faPlus)
This doesn't actually do any rendering. I can call icon(...).html and append that html to the document (and this works), but it doesn't seem to be the proper way to use the API (it is simply not clearly stated).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:此示例使用 Svelte 的 @html
这并不理想,但已经足够了。 (如果有人找到更好的解决方案,请告诉我!)
将类作为“类”选项传递到图标中也是不可取的,因为它没有 Tailwind 的智能感知。
示例:
注意手动导入 CSS,并阻止 Font Awesome 自动添加 CSS,以避免 FOUC(无样式内容的闪烁):
Note: This example is with Svelte's @html
This is not ideal but is adequate. (If someone figures out a better solution, please let me know!)
Passing the class as a "classes" option into icon is also undesirable because it won't have Tailwind's intellisense.
Example:
Take note to import the CSS manually and prevent Font Awesome's automatic adding of CSS to avoid a FOUC (flash of unstyled content):
使用 Fa Svelte 组件 添加 Font Awesome 图标:
这是组件内部添加/渲染的方式从
@fortawesome/free-solid-svg-icons
导入:https://github.com/Cweili/svelte-fa/blob/ master/src/fa.svelteAdd Font Awesome icons with the Fa Svelte component:
This is how the component internally adds/renders the import from
@fortawesome/free-solid-svg-icons
: https://github.com/Cweili/svelte-fa/blob/master/src/fa.svelte