简体   繁体   English

仅在另一个自定义元素中定义自定义元素

[英]Define custom element within another custom element only

We know that we can register our custom element like this:我们知道我们可以像这样注册我们的自定义元素:

customElements.define('x-my-element', MyElement);

And now, this element is available like so:现在,这个元素可以像这样使用了:

<x-my-element>Woo</x-my-element>

However, is there some sort of a scoped CustomElementRegistry for a specific ShadowRoot?但是,对于特定的 ShadowRoot 是否有某种范围内的 CustomElementRegistry?

I'm working on a browser plugin that needs to inject some custom elements into a page, but I want to avoid accidentally colliding with custom elements that the host page may have defined.我正在开发一个需要将一些自定义元素注入页面的浏览器插件,但我想避免意外地与主机页面可能定义的自定义元素发生冲突。 If I define an outer custom element, and all elements within are defined to work only within that outer custom element, that solves the problem.如果我定义了一个外部自定义元素,并且其中的所有元素都被定义为仅在该外部自定义元素中工作,那么问题就解决了。

(As an alternative, I'll probably just randomly generate element names with a build script.) (作为替代方案,我可能只是使用构建脚本随机生成元素名称。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM