简体   繁体   English

自定义元素构建时获取 this.textContent

[英]Obtain this.textContent during Custom Element Construction

This is a conceptual question for those who understand custom elements without seeing concrete code examples.对于那些在没有看到具体代码示例的情况下理解自定义元素的人来说,这是一个概念性问题。

I'm creating a custom element that's display depends on its textContent.我正在创建一个自定义元素,该元素的显示取决于其 textContent。 In the custom element's constructor, if I try to access this.textContent it returns an empty string, even though the html of that custom element does indeed contain text.在自定义元素的构造函数中,如果我尝试访问this.textContent它会返回一个空字符串,即使该自定义元素的 html 确实包含文本。

In order to obtain the textContent during the custom element's construction, I enclosed my constructor code within a setTimeout and then was able to make my custom elements construction be base on its textContent .为了在自定义元素的构造过程中获取textContent ,我将构造函数代码包含在 setTimeout 中,然后能够使我的自定义元素构造基于其textContent

However, this felt a little hacky and I figured there was a more proper way to obtain textContent during an element's construction.但是,这感觉有点麻烦,我认为有一种更合适的方法可以在元素构建期间获取 textContent。 That's when I found Using the life Cycle Callbacks .那时我发现了Using the life Cycle Callbacks

The connectedCallback method allowed me to see textContent without putting a setTimeout into the custom element's constructor. connectedCallback方法允许我查看 textContent 而无需将 setTimeout 放入自定义元素的构造函数中。 However, this quote concerns me:然而,这句话让我担心:

connectedCallback: .连接回调:。 . . . . may happen before the element's contents have been fully parsed.可能在元素的内容被完全解析之前发生。

This worries me that if I have a lot of content inside the custom element, this.textContent may still return an empty string because all that text may not yet be "fully parsed".这让我担心,如果我在自定义元素中有很多内容, this.textContent可能仍会返回一个空字符串,因为所有这些文本可能尚未“完全解析”。

Is this worry justified?这种担心有道理吗? Is there a more sure way to obtain this.textContent as a basis for your custom element's construction?有没有更可靠的方法来获取this.textContent作为自定义元素构建的基础? Or, should I go with my initial solution of putting a setTimeout within the custom element's constructor?或者,我应该采用在自定义元素的构造函数中放置 setTimeout 的初始解决方案吗?

In your example textContent is DOM content在您的示例中textContent是 DOM 内容

The constructor should not (try to) access DOM, constructor不应(尝试)访问 DOM,
as it can be run from .createElement('your-element') when there is no DOM at all.因为当根本没有 DOM 时,它可以从.createElement('your-element')运行。
(or in a Server-Side-Rendering scenario) (或在服务器端渲染场景中)

connectedCallback runs before the whole DOM inside finished parsing. connectedCallback在整个 DOM 内部完成解析之前运行。 If you want to access its DOM content you have to wait till Element DOM is ready.如果你想访问它的 DOM 内容,你必须等到 Element DOM 准备好了。

Only in FireFox you can access Element DOM content;只有在 FireFox 中你才能访问 Element DOM 内容;
see: wait for Element Upgrade in connectedCallback: FireFox and Chromium differences请参阅: 在 connectedCallback 中等待元素升级:FireFox 和 Chromium 差异

For detailed analysis see: https://jsfiddle.net/CustomElementsExamples/n20bwckt/详细分析见: https : //jsfiddle.net/CustomElementsExamples/n20bwckt/
Note the difference when run in FireFox and Chrome.请注意在 FireFox 和 Chrome 中运行时的区别。

setTimeout( func , 0 )设置超时(功能,0)

is totally valid (but better not in the constructor ) and ensures your code runs when the EventLoop is empty, thus when all DOM is ready to be accessed.是完全有效的(但最好不在constructor )并确保您的代码在 EventLoop 为空时运行,从而在所有 DOM 准备好访问时运行。 You could also use requestAnimationFrame .您也可以使用requestAnimationFrame
All libraries do something similar under the hood to add updateComplete and the likes.所有库都在updateComplete做类似的事情来添加updateComplete等。
With bare-bones Custom Element API you have to write it yourself.使用基本自定义元素 API,您必须自己编写。

Note: The connectedCallback now triggers code that runs after a (potential) disconnectedCallback , so check for that with this.isConnected in your code.注意: connectedCallback现在会触发(潜在的) disconnectedCallback之后运行的代码,因此请在代码中使用this.isConnected进行检查。

For visual diagram when all callbacks run see:有关所有回调运行时的可视化图表,请参阅:
https://andyogo.github.io/custom-element-reactions-diagram/ https://andyogo.github.io/custom-element-reactions-diagram/

Note: if you don't specify any methods in your own Element, the methods from HTMLElement run.注意:如果您没有在自己的 Element 中指定任何方法,则运行 HTMLElement 中的方法。
So Elements without any declared constructor or connectedCallback are perfectly valid.所以没有任何声明的constructorconnectedCallback元素是完全有效的。
In IconMeister I only use attributeChangedCallbackIconMeister我只使用attributeChangedCallback

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

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