使用字体很棒6图标()API(软件包管理器)

发布于 2025-01-17 11:47:36 字数 1008 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2025-01-24 11:47:36

注意:此示例使用 Svelte 的 @html

<i class="text-4xl mx-4 fa-spin">{ @html icon(faSun).html }</i>

这并不理想,但已经足够了。 (如果有人找到更好的解决方案,请告诉我!)

将类作为“类”选项传递到图标中也是不可取的,因为它没有 Tailwind 的智能感知。

示例:

{ @html icon(faSun, { classes: "text-4xl" }).html }

注意手动导入 CSS,并阻止 Font Awesome 自动添加 CSS,以避免 FOUC(无样式内容的闪烁):

import '@fortawesome/fontawesome-svg-core/styles.css';
import { config } from '@fortawesome/fontawesome-svg-core';
config.autoAddCss = false;

Note: This example is with Svelte's @html

<i class="text-4xl mx-4 fa-spin">{ @html icon(faSun).html }</i>

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:

{ @html icon(faSun, { classes: "text-4xl" }).html }

Take note to import the CSS manually and prevent Font Awesome's automatic adding of CSS to avoid a FOUC (flash of unstyled content):

import '@fortawesome/fontawesome-svg-core/styles.css';
import { config } from '@fortawesome/fontawesome-svg-core';
config.autoAddCss = false;
情归归情 2025-01-24 11:47:36

使用 Fa Svelte 组件 添加 Font Awesome 图标:

<script>
  import Fa from 'svelte-fa'
  import { faPlus} from '@fortawesome/free-solid-svg-icons'
</script>

<Fa icon={faPlus} /> Flag

这是组件内部添加/渲染的方式从 @fortawesome/free-solid-svg-icons 导入:https://github.com/Cweili/svelte-fa/blob/ master/src/fa.svelte

Add Font Awesome icons with the Fa Svelte component:

<script>
  import Fa from 'svelte-fa'
  import { faPlus} from '@fortawesome/free-solid-svg-icons'
</script>

<Fa icon={faPlus} /> Flag

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文