Javascript 通过createElement创建WebComponent

Javascript 通过createElement创建WebComponent,javascript,web-component,Javascript,Web Component,我在使用createElement创建Web组件时遇到问题。我得到了这个错误: 未捕获的DomeException:构造“CustomElement”失败:结果不能有子级 在附录TODO中 类TodoCard扩展了HtmleElement{ 构造函数(){ 超级() this.innerHTML=` ${this.getAttribute('content')} ` } } window.customElements.define('todo-card',TodoCard) const to

我在使用createElement创建Web组件时遇到问题。我得到了这个错误:

未捕获的DomeException:构造“CustomElement”失败:结果不能有子级 在附录TODO中

类TodoCard扩展了HtmleElement{ 构造函数(){ 超级() this.innerHTML=`
  • ${this.getAttribute('content')}
  • ` } } window.customElements.define('todo-card',TodoCard) const todoList=document.getElementById('todo-list') const todoForm=document.getElementById('todo-form') const todoInput=document.getElementById('todo-input') 函数appendTodo(内容){ const todo=document.createElement('todo-card') todo.setAttribute('content',content) todoList.appendChild(todo) } todoForm.addEventListener('submit',e=>{ e、 预防默认值() appendTodo(todoInput.value) todoInput.value=“” }) 有什么想法吗? 谢谢。

    构造函数中设置DOM内容的自定义元素
    永远不能使用
    document.createElement()

    您将看到许多在构造函数中设置DOM内容的示例(包括我的示例)。
    不能使用
    document.createElement

    说明(): 使用时:

      <todo-card content=FOO></todo-card>
    
    构造函数运行时没有HTML接口(元素可能与DOM无关),
    因此,在构造函数中设置innerHTML会产生错误:

    未捕获的DomeException:构造“CustomElement”失败:结果不能有子级

    发件人:

    元素不能获得任何属性或子元素,因为这违反了使用createElement或createElementNS方法的使用者的期望。 通常,工作应尽可能推迟到connectedCallback

    shadowDOM是一个DOM 使用shadowDOM时,可以在构造函数中设置shadowDOM内容:

      constructor(){
        super().attachShadow({mode:"open"})
               .innerHTML = `...`;
      }
    
    正确的代码(无阴影):使用
    connectedCallback
    
    window.customElements.define(
    “待办事项卡”,
    类扩展HtmleElement{
    构造函数(){
    超级();
    //this.innerHTML=this.getAttribute(“内容”);
    }
    connectedCallback(){
    this.innerHTML=this.getAttribute(“内容”);
    }
    }
    );
    试一试{
    const todo=document.createElement(“todo卡”);
    todo.setAttribute(“内容”、“栏”);
    document.body.appendChild(todo);
    }捕获(e){
    控制台错误(e);
    }
    
    构造函数中设置DOM内容的自定义元素

    永远不能使用
    document.createElement()

    您将看到许多在构造函数中设置DOM内容的示例(包括我的示例)。
    不能使用
    document.createElement

    说明(): 使用时:

      <todo-card content=FOO></todo-card>
    
    构造函数运行时没有HTML接口(元素可能与DOM无关),
    因此,在构造函数中设置innerHTML会产生错误:

    未捕获的DomeException:构造“CustomElement”失败:结果不能有子级

    发件人:

    元素不能获得任何属性或子元素,因为这违反了使用createElement或createElementNS方法的使用者的期望。 通常,工作应尽可能推迟到connectedCallback

    shadowDOM是一个DOM 使用shadowDOM时,可以在构造函数中设置shadowDOM内容:

      constructor(){
        super().attachShadow({mode:"open"})
               .innerHTML = `...`;
      }
    
    正确的代码(无阴影):使用
    connectedCallback
    
    window.customElements.define(
    “待办事项卡”,
    类扩展HtmleElement{
    构造函数(){
    超级();
    //this.innerHTML=this.getAttribute(“内容”);
    }
    connectedCallback(){
    this.innerHTML=this.getAttribute(“内容”);
    }
    }
    );
    试一试{
    const todo=document.createElement(“todo卡”);
    todo.setAttribute(“内容”、“栏”);
    document.body.appendChild(todo);
    }捕获(e){
    控制台错误(e);
    }