javascript - 淘汰 if 和文本绑定
问题描述
我正在尝试在 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>
现在,当我在其他地方实例化 a 时,出现错误:
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 一样棒......
推荐阅读
- php - PHP 命名空间自动加载,需要作曲家吗?
- python - 烧瓶 API 格式
- android - Hilt 无法将 ContextImpl 转换为 Application
- python - 如何移动二次线?
- reactjs - 如何将 HOC 组件导航到 App 组件
- c# - 在 azure 上部署后 SignalR 无法正常工作。在 localhost 上工作
- reporting-services - SSAS/SSRS:没有百分比格式?
- python - 我如何知道 XGBoost 的正类值和负类值是哪个?
- python - 查询数据帧时的分配律
- sql - 前 12 个月间隔和 ISO 周的平均值