我试图将switchmap用于这样的http请求:
export class HomeComponent implements OnInit {
public data: any;
_subject = new Subject();
test: Observable<any>;
constructor(private _http: HttpClient) {}
ngOnInit() {
this._subject.next('http://api/test');
this.test = this._subject.pipe(
switchMap((param: string) => {
console.log(param); // should be http://api/test
return param;
}
)
);
console.log(this.test);
}
}
我想查看param的结果,但是我什么也没得到。我的代码有什么问题? 稍后,我想在一个方法上使用switchmap功能:
callData(url) {
this._subject.next(url);
this.test = this._subject.pipe(
switchMap((param: string) => {
console.log(param); // should be http://api/test
return param;
}
)
);
}
在每个请求之后,它应该取消上一个请求;