[英]Knockout 3.2 components with named templates?
我正在嘗試在淘汰賽3.2.0中使用新的組件系統。
目前沒有太多文檔,但這確實有效。
ko.components.register('price-input', {
template: '<span>price-input</span>'
})
但是, template
綁定允許您指定DOM中已存在的模板名稱,例如:
<script type="text/html" id="price_input">
<span>price-input</span>
</script>
然后你可以這樣做:
<div data-bind="template: {name: 'price_input'}"></div>
所以我嘗試了這個:
ko.components.register('price-input', {
template: {name: 'price_input'}
})
但它不起作用。 有沒有辦法在新組件中使用命名模板,或者它們必須內聯或加載AMD。
謝謝
編輯:在RP Niemeyer的回答之后,為了澄清這里的模板,我嘗試了他的回答:
<script type="text/html" id="ifx_price_input">
<h4>PRICE INPUT <span data-bind="text: value"></span></h4>
</script>
這是組件代碼:
ko.components.register('price-input', {
template: {element: 'ifx_price_input'}
})
它確實加載模板,但將其視為轉義字符串。
想法?
您可以傳遞一個element
屬性,該屬性可以是元素本身,也可以傳遞一個element
的id
,如:
template: { element: 'myTmpl' }
在v3.2.0 beta中,這個案例處理得不好,因此是InternalFX需要的hackery。
這將在v3.2.0 final中修復。 它將按預期工作 - 只需引用script
, template
或textarea
元素,其邏輯內容將被解釋為模板節點。
如果您感興趣,修復和測試它的提交在這里: https : //github.com/knockout/knockout/pull/1454
最后用一些hackery解決了這個問題...我希望淘汰賽開發者能夠更好地解決這個問題。
這有效。 基本上我只是手動加載模板文本並將其傳遞給register
功能。 所以它就好像是內聯的。
ko.components.register('price-input', {
template: $('#ifx_price_input').html()
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.