amis2/examples/components/WebComponent.jsx

30 lines
858 B
React
Raw Normal View History

2021-08-09 22:21:54 +08:00
/**
* web component 的示例
*/
import '@webcomponents/webcomponentsjs/custom-elements-es5-adapter';
2021-08-09 22:21:54 +08:00
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);