未捕获的类型错误:无法在typescript中读取 null 的属性“追加”

IT技术 reactjs typescript
2021-05-22 11:50:29

我正进入(状态:

未捕获的类型错误:无法在typescript中读取 null 的属性“追加”

export class UserForm {
  constructor(public parent: Element) {}

  template(): string {
    return `
        <div>
        <h1> User Form</h1>
        <input/>
        <div>
        `;

  }

  render(): void {
    const templateElement = document.createElement('template');
    templateElement.innerHTML = this.template();
    console.log(this.parent);
    this.parent.append(templateElement.content);
  }
}
1个回答

尝试分配一个初始值,如:

constructor(public parent: Element) {
  this.parent =[];
}