【发布时间】:2017-10-21 03:23:00
【问题描述】:
我想在我的 Angular 2 项目的服务中创建延迟以进行测试。更具体地说,我想要
this.myIatreioService.getIatreia()
延迟 1 秒。我的服务如下:
@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 功能,但是没用:
getIatreia(): Observable<Iatreio[]> {
setTimeout(() => {
return this.http
.get('http://localhost:3000/iatreia', { headers: this.headers })
.map(response => response.json() as Iatreio[]);
}, 1000);
}
我的问题有解决方案吗?如果没有,我可以将 setTimeout() 放在 Component 或 Server 中吗?预先感谢您的帮助!
【问题讨论】:
标签: angular