HTMLCollectionOf缺少“ NodeList”类型的以下属性

时间:2019-09-24 00:35:53

标签: angular typescript stenciljs stencil-component

我刚刚从模具0更新到1,并且在建立组件集合后出现此错误。

[ ERROR ]  TypeScript: ./src\components\layout\tabs\aep-tabs.tsx:24:5
       Type 'HTMLCollectionOf<Element>' is missing the following properties from type 'NodeList': forEach, entries,
       keys, values

 L23:  addTitleEvents() {
 L24:    this.tabTitleElements = this.tabs.getElementsByClassName('tab-title');
 L25:    [].forEach.call(this.tabTitleElements, (tab, pos) => {...

我的aep-tabs.tsx文件

import { Component, h, Listen, Element, State, Event, Method, EventEmitter } from '@stencil/core';

@Component({
  tag: 'aep-tabs',
  styleUrl: 'aep-tabs-style.scss'
})
export class AepTabs {
  @Element() tabs: HTMLElement;
  @State() initIndex: number = 0;
  @Event() tabClick: EventEmitter;
  private tabsElements;
  private tabsElementList: Array<any> = [];
  private tabTitleElements: NodeList; // <---- HERE

  componentWillLoad() {
    this.createTabsTitles(this.tabs.querySelectorAll('aep-tab'));
  }

  componentDidLoad() {
    this.addTitleEvents();
  }

  addTitleEvents() {

    // ERROR HERE 'this.tabTitleElements' is displaying the error message.
    this.tabTitleElements = this.tabs.getElementsByClassName('tab-title');
    [].forEach.call(this.tabTitleElements, (tab, pos) => {
      tab.addEventListener('click', () => {
        this.tabClick.emit(tab);
        this.activeTabs(pos);
      });
    });
    this.activeTabs(this.initIndex);
  }

  createTabsTitles(tabsList: NodeList) {
    this.tabsElements = tabsList;
    [].forEach.call(this.tabsElements, tab => {
      this.tabsElementList.push(
        <div class="tab-title" data-automation={tab.titletab.toString().toLowerCase()}>
          <span>{tab.titletab}</span>
        </div>
      );
    });
  }
  @Method()
  async activeTabs(index: number) {
    // ERROR HERE 'this.tabTitleElements' is displaying the error message
    this.tabTitleElements = this.tabs.getElementsByClassName('tab-title');
    [].forEach.call(this.tabTitleElements, (tab, pos) => {
      if (index === pos) {
        tab.classList.add('active');
        tab.classList.remove('inactive');
      } else {
        tab.classList.remove('active');
        tab.classList.add('inactive');
      }
    });

    [].forEach.call(this.tabsElements, (tab, pos) => {
      if (index === pos) {
        tab.classList.add('active');
        tab.classList.remove('inactive');
      } else {
        tab.classList.remove('active');
        tab.classList.add('inactive');
      }
    });
  }

  @Listen('clicktab')
  clicktabHandler(event: CustomEvent) {
    this.activeTabs(event.detail);
  }

  render() {
    return (
      <div class="aep-tabs">
        <nav>{this.tabsElementList}</nav>
        <div class="tab-container" data-automation="tab-container">
          <div class="tab-content">
            <slot />
          </div>
        </div>
      </div>
    );
  }
}

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "allowUnreachableCode": false,
    "declaration": false,
    "experimentalDecorators": true,
    "allowJs": true,
    "lib": ["dom", "es7", "dom.iterable"],
    "moduleResolution": "node",
    "module": "esnext",
    "target": "es2017",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "jsx": "react",
    "jsxFactory": "h"
  },
  "include": ["src", "types/jsx.d.ts"],
  "exclude": ["node_modules"]
}

节点版本:10 TSC版本:3.6.3

我已经更新了“ lib”,“ target”,也将nodeList转换成一个数组,但是它们都不起作用。有谁知道我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您应将代码从getElementsByClassName替换为querySelectorAll

// this.tabTitleElements = this.tabs.getElementsByClassName('tab-title'); // change this
this.tabTitleElements = this.tabs.querySelectorAll('.tab-title');