【问题标题】:Default/Overridable content for Aurelia template (template parts)Aurelia 模板的默认/可覆盖内容(模板部分)
【发布时间】:2016-10-13 06:51:23
【问题描述】:

我正在尝试深入了解 Aurelia 框架。我正在尝试创建一个可重用的寻呼机组件。

有没有办法提供一些默认内容,但如果组件的用户愿意,可以覆盖它?

例如,我的 pager.html 如下所示:

<template>
  <div class="pager-container">
    <content select="pager-beginning"></content>
  </div>
</template>

我的 pager-beginning.html 看起来像这样:

<template>
  <content>
    <button type="button">|<</button>
  </content>
</template>

我在想我应该能够做这样的事情:

<template>
  <require from="components/pager/pager"></require>
  <pager></pager>
</template>

并让生成的标记看起来像这样:

<div class="pager-container">
  <button type="button">|<</button>
</div>

或者我应该能够做这样的事情:

<template>
  <require from="components/pager/pager"></require>
  <pager>
    <pager-beginning><button type="button"><i class="glyphicon glyphicon-step-backward"></i></button></pager-beginning>
  </pager>
</template>

并让生成的标记看起来像这样:

<div class="pager-container">
  <button type="button"><i class="glyphicon glyphicon-step-backward"></i></button>
</div>

我的想法是我可以提供寻呼机的所有功能、pager.js 文件中特定于寻呼机的所有逻辑以及默认的 html 呈现,然后允许组件的用户覆盖html 如果他们愿意的话。


目前似乎正在发生的是 pager-beginning.html &lt;content&gt;&lt;/content&gt; 标记内的标记总是被替换。所以我得到的标记看起来像这样:

<div class="pager-container"></div>

我不知道如何为其提供“默认”内容功能。

【问题讨论】:

  • 请注意,根据 Shadow DOM 规范的最新更改,&lt;content&gt; 标签已在 Aurelia 版本 1 中替换为 &lt;slot&gt; 标签。 Aurelia documenation 和对应的blog post

标签: javascript aurelia


【解决方案1】:

使用“模板部件”功能。更多信息here(搜索“模板部件”)。

pager.html

<template>
  <div class="pager-container">
    <button type="button" click.delegate="gotoBeginning()">
      <template replaceable part="goto-beginning-button-content">|<</template>
    </button>
  </div>
</template>

app.html

<template>
  <require from="./components/pager/pager"></require>
  <pager>
    <template replace-part="goto-beginning-button-content">
      <i class="glyphicon glyphicon-step-backward"></i>
    </template>
  </pager>
</template>

【讨论】:

  • 非常感谢您的帮助。我喜欢新文档,但仍然无法找到我想要的内容。这真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 2018-06-21
  • 2014-01-21
  • 2017-01-15
  • 1970-01-01
  • 2012-03-15
  • 2019-08-01
  • 2020-08-23
  • 1970-01-01
相关资源
最近更新 更多