【问题标题】:Property 'forEach' does not exist on type 'NodeList'类型“NodeList”上不存在属性“forEach”
【发布时间】:2018-09-17 12:25:43
【问题描述】:

我在表格中使用 ng2-dragula 进行拖放。

  this.dragulaService.drop.subscribe(value => {     

  let questions_group = value[3] as HTMLTableRowElement        
  let SectionTwo:Array<string> = [];
  let QuestionId:Array<string> = [];
  let OrderNo:Array<number> = [];
  var list2 = questions_group.childNodes;      

  list2.forEach( 
    function(currentValue, currentIndex,listObj) { 

      if(currentIndex!=0){           
        let sectionName2 = currentValue.lastChild.textContent
        SectionTwo.push(sectionName2)
        QuestionId.push(currentValue.firstChild.textContent)
        OrderNo.push(currentIndex)

      }         
    },
    ''
  );   });

突然间,它开始给我一个错误,“'NodeList' 类型上不存在属性 'forEach'。”。在它工作正常之前我没有做任何更改。

【问题讨论】:

  • 在操场上运行良好 typescriptlang.org/play/… 你的 tsconfig.json 是什么样的?
  • { "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap": true, "声明”:false,“moduleResolution”:“node”,“emitDecoratorMetadata”:true,“experimentalDecorators”:true,“target”:“es5”,“typeRoots”:[“node_modules/@types”],“lib”: [“es2017”,“dom”]}}
  • @TitianCernicova-Dragomir 看起来像那样
  • 看起来不错,一个潜在的错误来源可能是你在lib 选项中遗漏了dom,但事实并非如此。它应该工作......
  • 如果你跳转到childNodes的声明,你看到了什么?应该是readonly childNodes: NodeListOf&lt;Node &amp; ChildNode&gt;

标签: angular typescript angular6 dragula angular-dragula


【解决方案1】:

我在 tsconfig.json 的 lib 中添加了属性“dom.iterable”,它起作用了

【讨论】:

    【解决方案2】:

    另一种方式:

    • 使用展开运算符:

      var list2 = [...questions_group.childNodes]; // Take a look of the syntax
      
      list2.forEach(function(currentValue, currentIndex,listObj) => {
         // Todo
      }) 
      

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 2021-08-05
      • 1970-01-01
      • 2019-08-21
      • 2018-11-09
      • 2018-03-03
      • 2019-05-13
      • 2016-08-13
      • 2020-11-01
      相关资源
      最近更新 更多