角度6-在getElementsByClassName上进行循环。错误:类型“ HTMLCollectionOf <element>”不是数组类型或字符串类型

时间:2018-10-27 11:21:50

标签: angular typescript for-loop angular6

昨天我写的代码运行正常,没有输出错误,今天我无法编译它,因为错误提示“ Type'HTMLCollectionOf'不是数组类型或字符串类型。”

Private Sub ProductDue_BeforeUpdate(Cancel As Integer)
    If [ProductDue] <> IsNotNull And (Not ([ProductType] = "IFL" Or [ProductType] = "3-TIER IPRN" Or [ProductType] = "CD IPRN")) Then
        MsgBox ("ProductDue only valid for ProductType = IFL, 3-TIER IPRN, and CD IPRN")
    End If
End Sub

如果我注释了这些行,请编译并添加它们,然后突然起作用(出现错误,但一切正常。)我离开工作时很奇怪...


由于this,我注意到当我在this.myClassCollection前面添加代码时,代码便开始工作

public myClassCollection: HTMLCollectionOf<Element> = document.getElementsByClassName('myClass');

//I added ": HTMLCollectionOf<Element>" part as I saw it seemed to work with NodeList

ngOnInit() {
    this.onChange();
    console.log(this.page);
}

@HostListener('window:resize') onResize() {
    this.onChange();
}

public onChange() {

    if (window.innerWidth <= 992) {

        for (const pageItem of this.myClassCollection) {
            pageItem.classList.add('hide');
        }

    } else {
        for (const pageItem of this.myClassCollection) {
            pageItem.classList.remove('hide');
        }
    }
}

但是我还是不明白为什么它以前起作用了?什么都没改变

0 个答案:

没有答案