ファイルをWebコンポーネントに読み込みます
9762 ワード
ロードファイルのWebコンポーネント
✔️ 外部テキストファイル読み込み
.txt
, .svg
, .html
✔️ コンテンツをDOMに注入する
陰で
<slot>
元素<load-file>
要素自体replaceWith
属性✔️ ' HTMLインポート'の代替品ではありません
<script>
実行されません✔️ は8行のコードで行われます:
ページに外部SVGファイルを置く方法は複数あります
ソースhttps://vecta.io/blog/best-way-to-embed-svg
👉 srcとして:
<img src="file.svg">
👉 オブジェクトとして:
<object type = "image/svg+xml" data="file.svg"></object>
👉 背景画像
background-image: url(file.svg)
👉 🎉 使用する
<load-file>
Webコンポーネント🎉 <load-file replaceWith src="//load-file.github.io/heart.svg"></load-file>
<load-file src="//load-file.github.io/heart.svg">
<style>
path:nth-child(2n+2) {
fill: GREEN; /* shadowDOM style does NOT style global DOM */
}
</style>
</load-file>
replaceWith
属性<ロードファイル> Webコンポーネントの読み込み
要素をREPOから読み込む
<script src="https://load-file.github.io/element.js"></script>
カスタム要素が定義されるとき、それは重要でありません.既存の
<load-file>
カスタム要素が後で定義されると、ドキュメント内の要素が自動的にアップグレードされます.または<文書>タグを使ってHTML文書の< Head >の要素全体を定義します.
<script>
customElements.define("load-file",class extends HTMLElement{async connectedCallback(){
this.shadowRoot||this.attachShadow({mode:"open"});this.shadowRoot.innerHTML=await(
await fetch(this.getAttribute("src"))).text();this.shadowRoot.append(...this.children);
this.hasAttribute("replaceWith")&&this.replaceWith(...this.shadowRoot.children)}})
</script>
<ロードファイル> Webコンポーネントの使用
完全パスを指定する
src
属性追加する
replaceWith
属性がsrc内容に代わる<load-file>
文書内の要素そのもの<load-file replaceWith src="https://load-file.github.io/heart.svg"></load-file>
!年の資本に注意するreplaceWith
なしでreplaceWith
ソース内容はShadowdomで注入されます:<load-file src="https://load-file.github.io/heart.svg"></load-file>
属性を持つすべての要素shadowRoot
に移動します.<load-file src="https://load-file.github.io/heart.svg">
<style shadowRoot>
path:nth-child(2n+2) {
fill: GREEN; /* shadowDOM style will NOT style global DOM */
}
</style>
</load-file>
Reference
この問題について(ファイルをWebコンポーネントに読み込みます), 我々は、より多くの情報をここで見つけました https://dev.to/dannyengelman/load-file-web-component-add-external-content-to-the-dom-1ndテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol