javascript - 打字稿,'NodeListOf' 不是数组类型或字符串类型
问题描述
将我的 JS 转换为 TS 严格模式。
以下语法对我来说看起来不错,但 TS 在for
循环中抱怨allSubMenus
:
[ts] Type 'NodeListOf<Element>' is not an array type or a string type.
我错过了什么?
function subAct(target:Node){
const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems')
for (const sub of allSubMenus){
sub.classList.remove('active')
}
}
解决方案
您需要将target
编译器选项设置为es6
或更高NodeListOf<T>
才能进行迭代。
推荐阅读
- android - 依赖项是最新的,但 Gradle 认为不是
- angular6 - FormArray 得到 ExpressionChangedAfterItHasBeenCheckedError
- javascript - 当我添加用于在日历中显示事件的脚本时,事件在 fullcalendar 上不起作用
- javascript - Firebase 数据库同步调用问题
- wordpress - Wordpress 管理栏上的图标上的正方形
- java - 从第三方网络应用程序中识别 Google 用户
- php - 在 PHP 中从复杂的 XML 输出创建和回显数组
- java - 由于源 1.5,无法构建包目标
- python - 用python字符串中的上标替换每个插入符号
- python - 如何编写sqlite查询以避免python中的一对多数据库模型循环?