英文:
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 -->
<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>
<!-- 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.
<script src="https://kit.fontawesome.com/my id.js"crossorigin="anonymous"></script>
However, now I learnt web components. I created a header.js file for my header.
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();
// Create a shadow root
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
window.customElements.define("header-section", 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("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>`;
答案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('my-fa-element', class extends HTMLElement {
constructor() {
let href = "//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.0/css/all.min.css";
super().attachShadow({mode:'open'})
.innerHTML = `<style>@import url('${href}')</style>` +
[`check`,`square-check`,`circle-check`,`gear`]
.map(icon=>`fa-${icon}: <i class="fa fa-${icon}"></i><br>`).join("");
if (!document.querySelector(`link[href="${href}"]`)) {
document.head.append(
Object.assign(document.createElement("link"), {
rel: "stylesheet",
href
}))
};
}
});
<!-- language: lang-html -->
<my-fa-element></my-fa-element>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论