该问题通常发生在使用fetch函数时,fetch使用了错误的上下文对象。在Angular中,使用Http服务可以代替fetch函数。
代码示例:
import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map';
@Injectable() export class DataService { constructor(private http: Http) {}
getData(): Observable
在上述代码中,我们使用了Angular的Http服务来获取数据,而不是使用fetch函数。对于每个HTTP请求,Http服务都会使用正确的上下文对象来处理请求。这就避免了在使用fetch函数时出现的错误。