簡體   English   中英

淘汰賽如果和文本綁定

[英]knockout if and text binding

我正在嘗試在 Knockout (3.3.0) 中創建一個簡單的組件:

ko.components.register('test', {
    viewModel: function() {
        this.test = 'hello'
    },
    template:
        `<span data-bind='if: 1, text: test'></span>`
});
ko.applyBindings();

小提琴

現在,當我在其他地方實例化<test></test> ,出現錯誤:

Multiple bindings (if and text) are trying to control descendant bindings of the same element. You cannot use these bindings together on the same element.

這真的不可能嗎? 如果你問我,這就像最基本的功能。 我知道我可以使用<!-- ko text -->但是如何設置其他屬性,如src並同時使用if

好的,我找到了(或至少是一種可能的)解決方案:使用<!-- ko if --><!-- /ko --> 這樣,模板可以寫成

<!-- ko if: 1-->
<span data-bind='text: test'></span>
<!-- /ko -->

我仍然不認為它是完美的,在 Vue 中我只會做<span v-if='1'>{{text}}</span> bam done,但我想不是這個世界上的一切都像維...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM