在 knockout 中我们可以创建 custom elements看起来像这样:
<flight-deals params='from: "lhr", to: "sfo"'></flight-deals>
HTML中自定义元素的定义仍然是work in progress以及using this today的部分流程是使用 document.registerElement
将自定义元素注册到 DOM。
但是,我在 knockout 文档中找不到有关这些方面的任何内容,当我调查我的 custom elements are registered 时,我找不到任何内容。调用ko.components.register
后通过knockout到DOM,结果不是。
因此,如果我在 knockout 中使用自定义元素,我是否还应该确保使用 document.registerElement
手动注册这些元素?事实上, knockout 还没有做到这一点,这让我有点困惑。
最佳答案
您不需要为现代浏览器和 IE9+ 做任何特殊的事情。
对于 IE6 - IE8 支持,您确实需要意识到这一点并使用一点点魔法。如the relevant documentation提及:
- HTML5-era browsers, which includes Internet Explorer 9 and later, automatically allow for custom elements with no difficulties.
- Internet Explorer 6 to 8 also supports custom elements, but only if they are registered before the HTML parser encounters any of those elements.
IE 6-8’s HTML parser will discard any unrecognized elements. To ensure it doesn’t throw out your custom elements, you must do one of the following:
- Ensure you call
ko.components.register('your-component')
before the HTML parser sees any<your-component>
elements- Or, at least call
document.createElement('your-component')
before the HTML parser sees any<your-component>
elements. You can ignore the result of the createElement call — all that matters is that you have called it.
关于html - 我应该手动将自定义 knockout 元素注册到 DOM 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35036700/