amis2/examples/components/WebComponent.jsx

28 lines
788 B
React
Raw Normal View History

2021-08-09 22:21:54 +08:00
/**
* web component 的示例
*/
class RandomNumber extends HTMLElement {
connectedCallback() {
const prefix = this.getAttribute('prefix') || '';
const shadow = this.attachShadow({mode: 'open'});
const text = document.createElement('span');
text.textContent = `${prefix}: ${Math.floor(Math.random() * 1000)}`;
shadow.appendChild(text);
setInterval(function () {
const count = `${prefix}: ${Math.floor(Math.random() * 1000)}`;
text.textContent = count;
}, 2000);
}
}
class WebContainer extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: 'open'});
2021-08-09 22:21:54 +08:00
shadow.innerHTML = 'web-container';
}
}
customElements.define('random-number', RandomNumber);
customElements.define('web-container', WebContainer);