示例工程 34-template-slot
34-template-slot/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>template 与 slot · 原生模板与插槽</title>
<meta name="description" content="用 template 保存惰性结构,用声明式 Shadow DOM 与 slot 组合内容。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 30px 20px 40px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 800px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 26px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 10px; font-size: clamp(30px, 6vw, 42px); line-height: 1.15; }
.lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 21px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; background: #fff; }
caption { padding: 8px; text-align: left; color: #475569; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; }
thead th { background: #f1f5f9; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.card {
display: block;
margin: 12px 0;
padding: 14px;
border: 1px solid #e2e8f0;
border-radius: 10px;
background: #fff;
}
.card h2 { margin: 0 0 6px; font-size: 18px; }
.card p { margin: 0; }
.card .meta { margin-top: 8px; color: #64748b; font-size: 13px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
pre { margin: 10px 0 0; padding: 14px; overflow-x: auto; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: 12px; line-height: 1.7; }
pre code { padding: 0; background: none; color: inherit; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
footer { margin-top: 28px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 18px 14px 32px; }
.site-header { flex-direction: column; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">WEB / 2026</span>
<nav aria-label="页内导航">
<a href="#template">template</a>
<a href="#slots">slot</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 34</p>
<h1>template 与 slot</h1>
<p class="lead">
<code>template</code> 保存惰性结构,<code>slot</code> 把外部内容放进组件内部。两者都不需要框架。
</p>
<section id="template">
<h2>1. template 是惰性的</h2>
<p>模板内容不会渲染、不会加载图片、不会执行脚本,直到被克隆或使用。</p>
<template id="row-template">
<tr>
<td>示例</td>
<td class="num">0</td>
<td class="num">0</td>
</tr>
</template>
<table>
<caption>使用模板生成的行(示例为静态展示)</caption>
<thead>
<tr><th scope="col">项目</th><th scope="col">数量</th><th scope="col">金额</th></tr>
</thead>
<tbody>
<tr><td>键盘</td><td class="num">2</td><td class="num">600</td></tr>
<tr><td>显示器</td><td class="num">1</td><td class="num">1800</td></tr>
</tbody>
</table>
<pre><code>const tpl = document.querySelector("#row-template");
const row = tpl.content.cloneNode(true);
tbody.append(row);</code></pre>
<p class="note">template 本身不显示;要显示时必须克隆到 DOM。</p>
</section>
<section id="slots">
<h2>2. slot 与声明式 Shadow DOM</h2>
<p>下面的卡片用 <code>shadowrootmode</code> 声明 Shadow DOM,并用命名 slot 接收标题。</p>
<div class="card">
<template shadowrootmode="open">
<style>
:host { display: block; }
h2 { margin: 0 0 6px; font-size: 18px; }
.body { color: #334155; }
.meta { margin-top: 8px; color: #64748b; font-size: 13px; }
</style>
<h2><slot name="title">默认标题</slot></h2>
<div class="body"><slot>默认内容</slot></div>
<div class="meta"><slot name="meta">默认说明</slot></div>
</template>
<span slot="title">订单摘要</span>
<p>共 3 件商品,合计 2,400 元。</p>
<span slot="meta">2026-11-28 更新</span>
</div>
<p>命名 slot 用 <code>slot="title"</code> 对应;默认 slot 接收没有 slot 属性的内容。</p>
</section>
<section id="fallback">
<h2>3. slot 回退内容</h2>
<div class="card">
<template shadowrootmode="open">
<style>
h2 { margin: 0 0 6px; font-size: 18px; }
.body { color: #334155; }
</style>
<h2><slot name="title">未命名卡片</slot></h2>
<div class="body"><slot>这个组件还没有内容。</slot></div>
</template>
</div>
<p>没有传入内容时,slot 显示自己的回退文字。</p>
</section>
<section id="rules">
<h2>4. 规则</h2>
<ul>
<li><code>template</code> 内容不在主文档树中,不会渲染。</li>
<li>克隆用 <code>template.content.cloneNode(true)</code>。</li>
<li>slot 只在 Shadow DOM 中生效。</li>
<li>命名 slot 与 <code>slot</code> 属性一一对应。</li>
<li>默认 slot 接收未分配的内容。</li>
<li>回退内容要写清楚,不能留空。</li>
</ul>
</section>
</main>
<footer>
<p>示例内容为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>