Polymer 就绪与 connectedCallback
Polymer ready vs connectedCallback
我想知道ready()
方法在聚合物中的重要性是什么,这种方法的准时初始化是什么意思,read()
和[=之间有什么区别12=] ???
他们都有相同的"goal"。提供一种在元素被放置在 dom.
之后做某事的方法
ready
是特定于聚合物的,而 connectedCallback
在规格中。
因此,如果您有普通的 Web 组件,则只有 connectedCallback 可用。对于 Polymer Elements,您仍然可以使用 ready(特别是如果您从 Polymer 1 转换元素)
然而,此处描述了一些时间差异:
https://www.polymer-project.org/2.0/docs/upgrade#custom-elements-apis
The ready callback, for one-time initialization, signals the creation
of the element's shadow DOM. In the case of class-based elements, you
need to call super.ready() before accessing the shadow tree.
The major difference between 1.x and 2.0 has to do with the timing of
initial light DOM distribution. In the v1 shady DOM polyfill, initial
distribution of children into is asynchronous (microtask) to
creating the shadowRoot, meaning distribution occurs after observers
are run and ready is called. In the Polymer 1.0 shim, initial
distribution occurred before ready.
以下应该有所帮助。
connectedCallback can be called more than once
...
Generally all setup work in connectedCallback should be balanced with associated teardown work in disconnectedCallback; if not it usually means connectedCallback is the wrong place to do it
...
At this point it's hard to contrive general a use case where ready is the only/best place to do work, although they do come up from time to time in very specific cases.
...
ready is called only once, so work done here based on a property value may become invalidated if the property value is changed at a later point by the user
见https://github.com/Polymer/polymer/issues/4108#issuecomment-258983943
我想知道ready()
方法在聚合物中的重要性是什么,这种方法的准时初始化是什么意思,read()
和[=之间有什么区别12=] ???
他们都有相同的"goal"。提供一种在元素被放置在 dom.
之后做某事的方法ready
是特定于聚合物的,而 connectedCallback
在规格中。
因此,如果您有普通的 Web 组件,则只有 connectedCallback 可用。对于 Polymer Elements,您仍然可以使用 ready(特别是如果您从 Polymer 1 转换元素)
然而,此处描述了一些时间差异: https://www.polymer-project.org/2.0/docs/upgrade#custom-elements-apis
The ready callback, for one-time initialization, signals the creation of the element's shadow DOM. In the case of class-based elements, you need to call super.ready() before accessing the shadow tree.
The major difference between 1.x and 2.0 has to do with the timing of initial light DOM distribution. In the v1 shady DOM polyfill, initial distribution of children into is asynchronous (microtask) to creating the shadowRoot, meaning distribution occurs after observers are run and ready is called. In the Polymer 1.0 shim, initial distribution occurred before ready.
以下应该有所帮助。
connectedCallback can be called more than once
...
Generally all setup work in connectedCallback should be balanced with associated teardown work in disconnectedCallback; if not it usually means connectedCallback is the wrong place to do it
...
At this point it's hard to contrive general a use case where ready is the only/best place to do work, although they do come up from time to time in very specific cases.
...
ready is called only once, so work done here based on a property value may become invalidated if the property value is changed at a later point by the user
见https://github.com/Polymer/polymer/issues/4108#issuecomment-258983943