示例工程 21-web-components
21-web-components/01-x-counter/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>21 · x-counter</title>
</head>
<body>
<x-counter step="2"></x-counter>
<p id="status"></p>
<script type="module">
import './x-counter.js';
import { run } from './main.js';
const el = document.querySelector('x-counter');
el.addEventListener('count-change', (event) => {
document.getElementById('status').textContent = `count=${event.detail.count}`;
console.log('count-change =>', event.detail.count, 'composed 可见于外层');
});
run();
</script>
</body>
</html>