Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': this constructor has already been used with this registry

左心房为你撑大大i 提交于 2019-12-11 15:27:24

问题


Here is the full code:-

class code extends HTMLElement{
    constructor(){
        super();
        const shadow = this.attachShadow({
            mode: 'open'
          });
        const code = document.createElement('code');
        code.textContent = super.textContent;
        shadow.appendChild(code);
    }
}
var Ω = (function() {
    'use strict';
  
    /**
     * Create the constructor
     * @param {String} selector The selector to use
     */
    var Constructor = function(selector) {
      if (!selector) return;
      if (selector === 'document') {
        this.elems = [document];
      } else if (selector === 'window') {
        this.elems = [window];
      } else {
        this.elems = document.querySelectorAll(selector);
      }
    };
  
    /**
     * Run a callback on each item
     * @param  {Function} callback The callback function to run
     */
    Constructor.prototype.each = function(callback) {
      if (!callback || typeof callback !== 'function') return;
      for (var i = 0; i < this.elems.length; i++) {
        callback(this.elems[i], i);
      }
      return this;
    };
  
    
    Constructor.prototype.register = function(className) {
      this.each(function(item) {
        customElements.define(item.tagName.toLowerCase(), className);
      });
      return this;
    };

    Constructor.prototype.css = function(style){
        this.each(function(item) {
            item.style.cssText = style;
          });
          return this;
    }
    Constructor.prototype.value = function(){
        this.each(function(item) {
            const val = item.value;
            return val;
          });
          return this;
    }
    Constructor.prototype.load = function(fn){
        window.onload = fn;
        return fn;
    }
    /**
     * Instantiate a new constructor
     */
    var instantiate = function(selector) {
      return new Constructor(selector);
    };
  
    /**
     * Return the constructor instantiation
     */
    return instantiate;
  })();
  Ω('lol-bar').register(code);
Ω('check-1').register(code);
<lol-bar>Hey</lol-bar><br />
<check-1>This should look like the above one</check-1>

I had expected that this will output both the tags lol-bar and check-1 the same format but this thingy says indirectly that I'm allowed to use a constructor only once and hence further definitions can't get into reality. Someone please solve this and get me outta this paradox. Help and answers appreciated. Would greatly help in development of custags.js, an open source project of mine.

来源:https://stackoverflow.com/questions/58155479/uncaught-domexception-failed-to-execute-define-on-customelementregistry-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!