是否可以在运行时动态定义自定义组件模板内的元素类型?在运行时更改元素类型
我想,以避免在下面的示例中的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元素,但只有自定义组件,是否正确?
我问过的奥里利亚本的Gitter问题,其中埃里克Lieben建议使用@processContent(function)
装饰,取代指定function
内的内容,并返回true
让Aurelia路上对其进行处理。
不幸的是,我不知道如何实际应用这些说明,并希望在这里找到一些替代方法或有关如何实际完成此操作的一些细节。
编辑
我创建了一个相应的feature request。尽管已经提供了可能的解决方案,但是我希望看到一些更简单的方法来解决这个问题;)
'processContent'不会在这种情况下帮助,因为它不会有访问视图模型的属性(如'isLinkBtn') 。 –