Created
August 9, 2022 07:35
-
-
Save ciming/c6d775d332cd1fd681d9cdb60274e91a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 基本 | |
class PonyComponent extends HTMLElement { | |
constructor() { | |
super(); | |
console.log("I'm a pony"); | |
} | |
connectedCallback() { | |
this.innerHTML = '<h1>General Sola</h1>'; | |
} | |
} | |
customElements.define('ns-pony', PonyComponent); | |
// shadow elemety | |
class PonyComponent extends HTMLElement { | |
constructor() { | |
super(); | |
console.log("I'm a pony"); | |
const shadow = this.attachShadow({ mode: 'open' }); | |
const title = document.createElement('h1'); | |
title.textContent = 'General Sola'; | |
shadow.appendChild(title); | |
} | |
} | |
// 使用模板 | |
/** | |
<template id="pony-template"> | |
<style> | |
h1 { | |
color: blue; | |
} | |
</style> | |
<h1>General Sola</h1> | |
</template> | |
*/ | |
class PonyComponent extends HTMLElement { | |
constructor() { | |
super(); | |
console.log("I'm a pony"); | |
const template = document.querySelector('#pony-template'); | |
const title = document.importNode(template.content, true); | |
const shadow = this.attachShadow({ mode: 'open' }); | |
shadow.appendChild(title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment