我尝试在Knockout(3.3.0)中创建一个简单的组件:
ko.components.register('test', {
viewModel: function() {
this.test = 'hello'
},
template:
`<span data-bind='if: 1, text: test'></span>`
});
ko.applyBindings();
请参阅fiddle。
现在,当我在其他地方示例化<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
呢?
1条答案
按热度按时间xe55xuns1#
好吧,我找到了(或者至少是一个可能的)解决方案:使用
<!-- ko if --><!-- /ko -->
。这样,模板就可以写成我仍然不认为它是完美的,在Vue中我只会做
<span v-if='1'>{{text}}</span>
bam done,但我想不是这个世界上的一切都可以像Vue一样棒...