typescript – Angular 2调用setInterval()undefined Services表单依赖注入

前端之家收集整理的这篇文章主要介绍了typescript – Angular 2调用setInterval()undefined Services表单依赖注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过使用setInterval()每10分钟调用一个函数,在这个函数中,我想使用从角度2的依赖注入器得到的一个服务(称为auth),问题是控制台告诉我:

EXCEPTION: TypeError: this.auth is undefined

constructor(private auth: AuthService){
    setInterval(function(){ this.auth.refreshToken(); },1000 * 60 * 10);
  }
这在给setInterval的函数调用时不指向类.

改用箭头功能.

constructor(private auth: AuthService){
    setInterval(() => { this.auth.refreshToken(); },1000 * 60 * 10);
  }
原文链接:https://www.f2er.com/angularjs/140522.html

猜你在找的Angularjs相关文章