【发布时间】:2017-03-02 13:45:30
【问题描述】:
是否可以在运行时动态定义自定义组件模板内的元素类型?
我想避免在以下示例中重复 button 和 a 元素的内部内容:
<template>
<button if.bind="!isLinkBtn">
<span class="btn-icon">${icon}</span>
<span class="btn-text">${contentText}</span>
</button>
<a if.bind="isLinkBtn">
<!--
The content is a 1:1 duplicate of the button above which should be prevented
somehow in order to keep the view DRY
-->
<span class="btn-icon">${icon}</span>
<span class="btn-text">${contentText}</span>
</a>
</template>
是否可以这样写:
<template>
<!--
The type of element should be defined at runtime and can be a standard HTML "button"
or an anchor "a"
-->
<element type.bind="${isLinkBtn ? 'a' : 'button'}">
<span class="btn-icon">${icon}</span>
<span class="btn-text">${contentText}</span>
</element>
</template>
我知道使用 <compose view="${widget.type}-view.html"></compose> 进行动态组合,但据我所知,这不允许我创建默认 HTML 元素,而只能创建自定义组件,对吗?
我在 Aurelia Gitter 上问过这个问题,Erik Lieben 建议使用 @processContent(function) 装饰器,替换给定 function 中的内容并返回 true 以让 Aurelia 处理它。
不幸的是,我不知道如何实际应用这些说明,我希望这里有一些替代方法或有关如何实际完成此操作的一些详细信息。
编辑
我已经创建了一个对应的feature request。尽管已经提供了可能的解决方案,但我希望看到一些更简单的方法来解决这个问题;)
【问题讨论】:
-
processContent在这种情况下无济于事,因为它无法访问视图模型的属性(如isLinkBtn)。
标签: aurelia aurelia-templating