Angular 2-在服务中设置超时 [英] Angular 2 - Setting Timeout in a Service
本文介绍了Angular 2-在服务中设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Angular 2项目的Service中创建延迟以进行测试.更具体地说,我想致电
I want to create a delay in a Service of my Angular 2 project for testing purposes. More specifically, I want the call of
this.myIatreioService.getIatreia()
要延迟1秒.我的服务如下:
to be delayed for 1 sec. My service is the following:
@Injectable()
export class IatreioService {
private headers = new Headers();
constructor(private http: Http) {
let token = localStorage.getItem('token');
if (token) {
this.headers = new Headers({ 'Authorization': 'Bearer ' + token });
}
}
getIatreia(): Observable<Iatreio[]> {
return this.http
.get('http://localhost:3000/iatreia', { headers: this.headers })
.map(response => response.json() as Iatreio[]);
}
} // end of Service
我尝试实现setTimeout函数,但是它不起作用:
I have tried to implement setTimeout function, but it doesn't work:
getIatreia(): Observable<Iatreio[]> {
setTimeout(() => {
return this.http
.get('http://localhost:3000/iatreia', { headers: this.headers })
.map(response => response.json() as Iatreio[]);
}, 1000);
}
我的问题有解决方案吗?如果没有,我可以将setTimeout()放在组件中还是在服务器中?预先感谢您的帮助!
Is there a solution for my problem?? If not, can I put setTimeout() either in Component or in Server?? Thank you in advance for your help!
推荐答案
您可以简单地使用 查看全文