“import font awesome in web component” 可以翻译为 “在 Web 组件中导入 Font Awesome”。

huangapple go评论83阅读模式
英文:

import font awesome in web component

问题

这是我的标题组件:

<header class="header">
  <div class="icon-container">
    <ul>
      <li class="list-item"><i class="fa-regular fa-bell"></i></li>
      <li class="list-item"><i class="fa-solid fa-gear"></i></li>
      <li class="list-item"><i class="fa-solid fa-arrow-right-from-bracket"></i></li>
    </ul>
  </div>
</header>

这段代码可以在我将 font-awesome 导入到我的 index.html 文件中的闭合 body 标签之前时显示 fontawesome 图标:

<script src="https://kit.fontawesome.com/my id.js" crossorigin="anonymous"></script>

然而,现在我学习了 web 组件。我为我的标题创建了一个 header.js 文件:

const template = document.createElement("template");
template.innerHTML = `
<div class="icon-container">
  <ul>
    <li class="list-item"><i class="fa-regular fa-bell"></i></li>
    <li class="list-item"><i class="fa-solid fa-gear"></i></li>
    <li class="list-item"><i class="fa-solid fa-arrow-right-from-bracket"></i></li>
  </ul>
</div>`;
class header extends HTMLElement {
  constructor() {
    super();

    // 创建一个影子 DOM
    this.attachShadow({ mode: "open" });
    this.shadowRoot.appendChild(template.content.cloneNode(true));
  }
}
window.customElements.define("header-section", header);

现在,HTML 的其他部分仍然渲染,但图标消失了,您应该如何解决这个问题?

英文:

This is my header component :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;header class=&quot;header&quot;&gt;
  &lt;div class=&quot;icon-container&quot;&gt;
    &lt;ul&gt;
      &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-regular fa-bell&quot;&gt;&lt;/i&gt;&lt;/li&gt;
      &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-solid fa-gear&quot;&gt;&lt;/i&gt;&lt;/li&gt;
      &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-solid fa-arrow-right-from-bracket&quot;&gt;&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/header&gt;

<!-- end snippet -->

This code can display the icons in fontawesome when I import the font-awesome before the closing body tag in my index.html file.

&lt;script src=&quot;https://kit.fontawesome.com/my id.js&quot;crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

However, now I learnt web components. I created a header.js file for my header.

const template = document.createElement(&quot;template&quot;);
template.innerHTML = `
&lt;div class=&quot;icon-container&quot;&gt;
      &lt;ul&gt;
        &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-regular fa-bell&quot;&gt;&lt;/i&gt;&lt;/li&gt;
        &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-solid fa-gear&quot;&gt;&lt;/i&gt;&lt;/li&gt;
        &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-solid fa-arrow-right-from-bracket&quot;&gt;&lt;/i&gt;&lt;/li&gt;
      &lt;/ul&gt;
&lt;/div&gt;`;
class header extends HTMLElement {
  constructor() {
    super();

    // Create a shadow root
    this.attachShadow({ mode: &quot;open&quot; });
    this.shadowRoot.appendChild(template.content.cloneNode(true));
  }

window.customElements.define(&quot;header-section&quot;, header);

Now, the other part of html is still rendering, but the icon disappear, so how should I solve this?

答案1

得分: 2

由于在您的网络组件中未加载font-awesome图标字体,因此图标未显示。要解决此问题,您可以在模板中使用链接标签加载font-awesome到您的网络组件中。

以下是如何修改您的模板:

const template = document.createElement("template");
template.innerHTML = `
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-jkihXtPklFJy8q3rnoEMBstczhBwFwO48lEQ7EDKlA5JX7xu5Q0NVg7lLeK/cHhV7hly0iygDRNyo6N88U6B9g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="icon-container">
    <ul>
        <li class="list-item"><i class="fa-regular fa-bell"></i></li>
        <li class="list-item"><i class="fa-solid fa-gear"></i></li>
        <li class="list-item"><i class="fa-solid fa-arrow-right-from-bracket"></i></li>
    </ul>
</div>`;

请注意,这是您模板的翻译。

英文:

Since the font-awesome icon fonts are not being loaded in your web component, the icons are not being displayed. To solve this, you can load the font-awesome in your web component using the link tag in your template.

Here's how you can modify your template:

const template = document.createElement(&quot;template&quot;);
template.innerHTML = `
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot; integrity=&quot;sha512-jkihXtPklFJy8q3rnoEMBstczhBwFwO48lEQ7EDKlA5JX7xu5Q0NVg7lLeK/cHhV7hly0iygDRNyo6N88U6B9g==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;
&lt;div class=&quot;icon-container&quot;&gt;
    &lt;ul&gt;
        &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-regular fa-bell&quot;&gt;&lt;/i&gt;&lt;/li&gt;
        &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-solid fa-gear&quot;&gt;&lt;/i&gt;&lt;/li&gt;
        &lt;li class=&quot;list-item&quot;&gt;&lt;i class=&quot;fa-solid fa-arrow-right-from-bracket&quot;&gt;&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;`;

答案2

得分: 2

在 shadowDOM 中使用时,你需要在 shadowDOM 和 globalDOM 中都加载 CSS。

让你的组件检查链接是否存在于 globalDOM 中,
如果不存在,Web 组件将添加该 href。
英文:

for use in shadowDOM you need to load the CSS both in shadowDOM and globalDOM.

Make your component check if the link exists in globalDOM,
if not; the Web Component adds the href

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

customElements.define(&#39;my-fa-element&#39;, class extends HTMLElement {
  constructor() {
    let href = &quot;//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.0/css/all.min.css&quot;;
    super().attachShadow({mode:&#39;open&#39;})
           .innerHTML = `&lt;style&gt;@import url(&#39;${href}&#39;)&lt;/style&gt;` +
           [`check`,`square-check`,`circle-check`,`gear`]
             .map(icon=&gt;`fa-${icon}: &lt;i class=&quot;fa fa-${icon}&quot;&gt;&lt;/i&gt;&lt;br&gt;`).join(&quot;&quot;);
    if (!document.querySelector(`link[href=&quot;${href}&quot;]`)) {
      document.head.append(
        Object.assign(document.createElement(&quot;link&quot;), {
          rel: &quot;stylesheet&quot;,
          href
        }))
    };
  }
});

<!-- language: lang-html -->

&lt;my-fa-element&gt;&lt;/my-fa-element&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 00:33:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定