我正在尝试在 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
同时?
最佳答案
好的,我找到了(或者至少是一种可能的)解决方案:使用 <!-- ko if --><!-- /ko -->
。这样模板就可以这样写
<!-- ko if: 1-->
<span data-bind='text: test'></span>
<!-- /ko -->
我仍然不认为它是完美的,在 Vue 中我会这样做 <span v-if='1'>{{text}}</span>
砰的一声完成了,但我想这个世界上并不是所有的东西都能像 Vue 一样棒......
关于javascript - knockout if 和文本绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60576207/