我想用setInterval()每10分钟调用一个函数,在这个函数中,我想使用Angular 2的依赖注入程序提供的服务(名为auth),问题是控制台告诉我:
例外:TypeError:这个.auth未定义
constructor(private auth: AuthService){
setInterval(function(){ this.auth.refreshToken(); }, 1000 * 60 * 10);
}
我想用setInterval()每10分钟调用一个函数,在这个函数中,我想使用Angular 2的依赖注入程序提供的服务(名为auth),问题是控制台告诉我:
例外:TypeError:这个.auth未定义
constructor(private auth: AuthService){
setInterval(function(){ this.auth.refreshToken(); }, 1000 * 60 * 10);
}
当类中的设置点没有被调用时.
改用箭头功能.
constructor(private auth: AuthService){
setInterval(() => { this.auth.refreshToken(); }, 1000 * 60 * 10);
}