【问题标题】:Angular call function inside fetch (ngOninit)fetch 中的 Angular 调用函数 (ngOninit)
【发布时间】:2022-01-14 06:59:31
【问题描述】:

我想在 fetch 中调用一个函数,但由于某种原因,我收到以下错误并且找不到解决方案。

 Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'myFunction')
TypeError: Cannot read properties of undefined (reading 'myFunction')

我应该如何在 fetch 中调用一个函数 -> 那么呢? 极简主义复制在下面的链接:

https://plnkr.co/edit/kizI9pLOe8XZzKYz?preview

【问题讨论】:

    标签: angular fetch function-call uncaught-exception ngoninit


    【解决方案1】:

    当你声明一个函数时,你的上下文会丢失。

    使用箭头函数来避免该错误如下:

    fetch('https://www.ag-grid.com/example-assets/row-data.json').then( (response) => {
      return response.json();
    }).then( (data) =>{
      this.myFunction(data);
    })
    

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 2017-11-20
      • 2016-06-26
      • 1970-01-01
      • 2020-08-23
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多