2017-05-07 9 views
5

में रखा गया है 'CustomElement' त्रुटि बनाने में विफल:मैं एक कस्टम तत्व तो तरह परिभाषित किया है जब JavaScript फ़ाइल सिर

class SquareLetter extends HTMLElement { 
    constructor() { 
     super(); 
     this.className = getRandomColor(); 
    } 
} 
customElements.define("square-letter", SquareLetter); 

जब JavaScript फ़ाइल एचटीएमएल <head> टैग में शामिल किया गया है, क्रोम console रिपोर्ट इस त्रुटि :

Uncaught DOMException: Failed to construct 'CustomElement': The result must not have attributes

लेकिन जब JavaScript फ़ाइल </body> समाप्त होने के टैग से पहले शामिल है, सब कुछ ठीक काम करता है। क्या कारण है?

<head> 
    <script src="js/SquareLetter.js"></script> <!-- here --> 
</head> 
<body> 
    <square-letter>A</square-letter> 
    <script src="js/SquareLetter.js"></script> <!-- or here --> 
</body> 
+3

संभावित डुप्लिकेट [अपने कन्स्ट्रक्टर से कस्टम तत्व के गुणों तक नहीं पहुंच सकता] [http://stackoverflow.com/questions/42251094/cannot-access-attributes-of-a- कस्टम-element-from-its-constructor) – Supersharp

उत्तर

6

त्रुटि सही है बदला जा सकता है, और दोनों ही मामलों में हो सकता है। आप भाग्यशाली हो रहे हैं क्योंकि कस्टम तत्वों के कुछ वर्तमान कार्यान्वयन इस आवश्यकता को लागू नहीं करते हैं।

कस्टम तत्व के लिए निर्माता को अपने डोम को पढ़ने या लिखना नहीं है। यह बाल तत्व नहीं बना सकता है, या विशेषताओं को संशोधित नहीं करना चाहिए। उस काम को बाद में connectedCallback() विधि में किया जाना चाहिए (हालांकि ध्यान दें कि connectedCallback() को तत्व हटा दिया गया है और डीओएम में दोबारा जोड़ा गया है, तो आपको इसे जांचने की आवश्यकता हो सकती है, या इसमें बदलाव पूर्ववत करना पड़ सकता है disconnectedCallback())।

WHATWG HTML विनिर्देश का हवाला देते हुए, जोर मेरा:

§ 4.13.2 Requirements for custom element constructors :

When authoring custom element constructors, authors are bound by the following conformance requirements:

  • A parameter-less call to super() must be the first statement in the constructor body, to establish the correct prototype chain and this value before any further code is run.

  • A return statement must not appear anywhere inside the constructor body, unless it is a simple early-return (return or return this).

  • The constructor must not use the document.write() or document.open() methods.

  • The element's attributes and children must not be inspected, as in the non-upgrade case none will be present, and relying on upgrades makes the element less usable.

  • The element must not gain any attributes or children, as this violates the expectations of consumers who use the createElement or createElementNS methods.

  • In general, work should be deferred to connectedCallback as much as possible—especially work involving fetching resources or rendering. However, note that connectedCallback can be called more than once, so any initialization work that is truly one-time will need a guard to prevent it from running twice.

  • In general, the constructor should be used to set up initial state and default values, and to set up event listeners and possibly a shadow root.

Several of these requirements are checked during element creation, either directly or indirectly, and failing to follow them will result in a custom element that cannot be instantiated by the parser or DOM APIs. This is true even if the work is done inside a constructor-initiated microtask, as a microtask checkpoint can occur immediately after construction.

जब आप डोम में तत्व के बाद, आप मौजूदा तत्वों "अपग्रेड" प्रक्रिया के माध्यम से जाने के लिए पैदा करने के लिए स्क्रिप्ट चलते हैं। जब स्क्रिप्ट तत्व से पहले होती है, तो तत्व मानक निर्माण प्रक्रिया के माध्यम से जाता है। यह अंतर स्पष्ट रूप से त्रुटि को सभी मामलों में प्रकट नहीं कर रहा है, लेकिन यह एक कार्यान्वयन विवरण है और बदल सकता है।

-1

तत्व स्क्रिप्ट लोड अभी तक लोड नहीं किया है तो यह बदला नहीं जा सकता, नीचे दिए गए तत्व का मतलब यह

संबंधित मुद्दे