微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/angularjs/140522.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐