实现说明
<template> 承载惰性标记,克隆进文档后才成为活动内容。
<slot> 命名影子树中 light-DOM 子节点的插入点。
常见问题
标记里用了哪些非通用元素?
<template>、<h3>、<slot>、<p>、<my-card>——每个都带有通用容器没有的原生语义或浏览器行为。
核心语句
<template id="card-template">
<h3><slot name="title">Default Title</slot></h3>
<p><slot>Default content</slot></p>
代码示例
<template id="card-template">
<style>
.card { border: 1px solid #ccc; border-radius: 8px; padding: 16px; }
h3 { margin: 0 0 8px; }
</style>
<div class="card">
<h3><slot name="title">Default Title</slot></h3>
<p><slot>Default content</slot></p>
</div>
</template>
<my-card>
<span slot="title">Custom Title</span>
Custom content here
</my-card>