是否可以在運行時動態定義自定義組件模板內的元素類型?在運行時更改元素類型
我想,以避免在下面的示例中的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') 。 –