javascript - Knockout components and other viewModels coexistence
问题描述
I've learnt that after registering a knockout component, one should call ko.applyBindings()
without argument. However, when defining another viewModel without components, let's call it vm
, one should call ko.applyBindings(new vm())
. Now when I try to use both in parallel, I get an error
You cannot apply bindings multiple times to the same element.
See also this fiddle
What can I do to make this work?
解决方案
我找到了答案。问题是你不能在applyBindings
没有第二个参数(也就是绑定范围的 HTML 元素)的情况下调用两次。对于我的组件,我很乐意提供第二个参数,即组件本身,但这在文档中不存在,您所能找到的只是applyBindings()
没有任何参数。所以,通过反复试验,我发现
ko.applyBindings(null, document.getElementById('id'))
作为组件 HTML 元素的id
id,有效。
推荐阅读
- java - 春季应用程序运行:失败
- c# - 如何无限次重复任务?
- redis - 使用 HSET 或 SETBIT 在 Redis 中存储 60 亿个 SHA256 哈希
- python-3.x - 为什么我的 LSTM 模型介于损失:0.3 到 0.5 和准确度:0.3 到 0.5 之间?喀拉斯
- c# - 错误:CS0029 无法隐式转换类型“System.Collections.Generic.List”
'到'X'? - javascript - 如何使用 JQuery 或 JavaScript 在 HTML 中的多个表之间添加一个表?
- node.js - 在 NextJS 中实现速率限制
- java - 无法单击“100”搜索结果类和 xpath - selenium webdriver java
- python-3.x - YOLO 输出图像数组的最后一维对应什么?
- javascript - 如何从数组中删除对象?