home
  • 博客
6.11
  • 简介
  • 入门
  • 教程
  • 核心概念
  • 组件
    • 模板即 HTML
    • 组件简介
    • 组件参数与 HTML 属性
    • 条件内容
    • 块内容
    • 辅助函数
    • 组件状态与动作
    • 遍历列表
    • 模板生命周期、DOM 与修饰符
    • 内置组件
    • 模板标签格式
  • 路由
  • 服务
  • EmberData
  • 深入主题
  • 应用程序开发
  • 应用程序关注点
  • 无障碍访问
  • 配置
  • 测试
  • 插件与依赖
  • 使用 TypeScript
  • 开发工具
  • 构建工具
  • Ember Inspector
  • 代码编辑器
  • 其他资源
  • 升级
  • 为 Ember.js 贡献代码
  • 术语表

模板即 HTML


Ember 的用户界面核心是由 HTML 驱动的 —— 用户看到的 UI 的每一部分,都定义在应用程序中某个组件的模板里。正因如此,模板是 Ember 的核心,也是该框架最重要的组成部分之一。

我们将在接下来的章节中讨论模板的功能和核心概念,但在此之前,我们需要从基础开始。开始构建 Ember 组件最简单的方法就是使用 HTML!

Application Route 组件

Ember 应用程序的核心组件是位于 app/templates/application.gjs 中的 application route 组件。.gjs 扩展名是“Glimmer JavaScript”的缩写,它允许我们将 JavaScript 和模板放在同一个文件中。我们可以将 HTML 复制到该文件的 <template> 部分,它无需任何修改即可运行。例如,您可以复制以下简单的消息应用程序的 HTML 示例

app/templates/application.gjs
<template>
  <div class="messages">
    <aside>
      <div class="avatar is-active" title="Tomster's avatar">T</div>
    </aside>
    <section>
      <h4 class="username">
        Tomster
        <span class="local-time">their local time is 4:56pm</span>
      </h4>

      <p>
        Hey Zoey, have you had a chance to look at the EmberConf brainstorming doc
        I sent you?
      </p>
    </section>

    <aside class="current-user">
      <div class="avatar" title="Zoey's avatar">Z</div>
    </aside>
    <section>
      <h4 class="username">Zoey</h4>

      <p>Hey!</p>

      <p>
        I love the ideas! I'm really excited about where this year's EmberConf is
        going, I'm sure it's going to be the best one yet. Some quick notes:
      </p>

      <ul>
        <li>
          Definitely agree that we should double the coffee budget this year (it
          really is impressive how much we go through!)
        </li>
        <li>
          A blimp would definitely make the venue very easy to find, but I think
          it might be a bit out of our budget. Maybe we could rent some spotlights
          instead?
        </li>
        <li>
          We absolutely will need more hamster wheels, last year's line was
          <em>way</em> too long. Will get on that now before rental season hits
          its peak.
        </li>
      </ul>

      <p>Let me know when you've nailed down the dates!</p>
    </section>

    <form>
      <label for="message">Message</label>
      <input id="message" />
      <button type="submit">Send</button>
    </form>
  </div>
</template>

您可以通过在终端运行 npm start 来服务(serve)该应用程序,这会让本地的应用程序副本在您的 Web 浏览器中可见。

如果您启动服务并在 Web 浏览器中访问 localhost:4200,您将看到渲染出的 HTML。目前它还没有样式。

要为应用程序设置样式,请将以下 CSS 复制到 app/styles/app.css 中

styles/app.css
body {
  max-width: 800px;
  margin: auto;
  padding: 2em;
  font-family: sans-serif;
  background-color: #fdfdfd;
}

.messages {
  display: grid;
  grid-template-columns: 80px 1fr;
  padding: 2em;
  border-radius: 0.5em;
  box-shadow: 0 0.25em 1.5em 0.25em rgba(0, 0, 0, 0.1);
}

.messages > section {
  margin-bottom: 1.5em;
  line-height: 1.5em;
}

.messages p,
.messages ul,
.username {
  margin: 0.5em 0;
}

.local-time {
  font-size: 0.8em;
  color: #da6c4d;
  font-weight: normal;
  margin-left: 10px;
}

.avatar {
  position: relative;
  border-radius: 50%;
  width: 60px;
  height: 60px;

  text-align: center;
  line-height: 60px;

  color: white;
  font-weight: bold;
  background-color: #ff907b;
}

.avatar.is-active:after {
  content: " ";
  height: 14px;
  width: 14px;
  border: solid 3px white;
  border-radius: 50%;
  background-color: #8bc34a;
  position: absolute;
  bottom: 0;
  right: 0;
}

.current-user .avatar {
  background-color: #30aba5;
}

form {
  display: grid;
  grid-template-columns: 1fr 6em;
  grid-template-rows: repeat(2, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  grid-column: span 2;
}

form > label {
  grid-area: 1 / 1 / 2 / 2;
}

form > input {
  padding: 0.5em;
  border-top-left-radius: 0.5em;
  border-bottom-left-radius: 0.5em;
  border: 1px solid #cccccc;
  border-right: none;
  font-size: 1em;
  grid-area: 2 / 1 / 3 / 2;
}

form > button {
  border-top-right-radius: 0.5em;
  border-bottom-right-radius: 0.5em;
  border: 1px solid #cccccc;
  font-size: 1em;
  grid-area: 2 / 2 / 3 / 3;
}

screenshot of styled message app

您可以使用 HTML 开始构建 Ember 应用程序的各个部分,因此,如果您已经了解 HTML 和 CSS,那么您就已经知道如何构建基本的 Ember 应用程序了!

您甚至可以无需修改地使用 SVG 或 Web Components。只要您的 HTML 是有效的,Ember 就会渲染它。

自闭合标签

除了标准的 HTML 语法外,Ember 还允许您使用自闭合语法 (<div />) 作为开始标签和结束标签 (<div></div>) 的简写。

Zoey 说……
对于像 img 或 br 这样在 HTML 规范中已经定义为自闭合的 "空(void)" HTML 标签,您无需使用此语法,但对于非自闭合标签,您可以使用此语法作为简写。

支持的特性

这意味着以下所有 HTML 特性均可照常工作

  • Web Components
  • SVG
  • HTML 注释
  • 空格(遵循与普通 HTML 相同的规则)
  • 像 <table> 和 <select> 这样的特殊 HTML 元素

限制

您可以放置在 Ember 模板中的 HTML 有一些小限制

  • 只能使用 <body> 标签内有效的 HTML 元素
  • 不能使用 <script> 标签

除此之外,尽管放心使用!

left arrow
回顾
组件简介
right arrow
本页内容

  • Application Route 组件
  • 自闭合标签
  • 支持的特性
  • 限制
团队 赞助商 安全 法律条款 品牌形象 社区准则
Twitter GitHub Discord Mastodon

如果你需要帮助,可以通过电子邮件联系我们,提交一个 issue,或者加入 Ember Discord 获取实时帮助。

© 版权所有 2026 - Tilde Inc.
Ember.js 是免费且开源的,并将永远保持免费。


Ember 由以下机构慷慨赞助
blue Created with Sketch.