【发布时间】:2019-01-29 10:14:34
【问题描述】:
我的代码:
var checkboxes = this.element.querySelectorAll("input[type=checkbox]") as NodeListOf<HTMLInputElement>;
checkboxes.forEach(ele => {
var key = ele.name;
if (data.hasOwnProperty(key)) {
if (!this.isArray(data[key])) {
var temp = data[key];
data[key] = [temp];
}
} else {
data[key] = [];
}
});
但我得到了一个错误:
错误 TS2339:类型上不存在属性“forEach” 'NodeListOf'。
interface NodeListOf<TNode extends Node> extends NodeList {
length: number;
item(index: number): TNode;
[index: number]: TNode;
}
interface NodeList {
/**
* Returns an array of key, value pairs for every entry in the list
*/
entries(): IterableIterator<[number, Node]>;
/**
* Performs the specified action for each node in an list.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(callbackfn: (value: Node, index: number, listObj: NodeList) => void, thisArg?: any): void;
/**
* Returns an list of keys in the list
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the list
*/
values(): IterableIterator<Node>;
[Symbol.iterator](): IterableIterator<Node>;
}
“NodeListOf”继承自“NodeList”,“NodeList”有“forEach” 方法,为什么'NodeListOf'上不存在'forEach'?
【问题讨论】:
-
因为它只存在于
Array。不知道你在这里寻找什么样的答案。如果您想使用forEach,请使用Array.from()。 -
@jhpratt
NodeList有一个forEach方法:NodeList.prototype.forEach
标签: javascript typescript