如何退订rxjs 5.1中Observable.create返回的Observable

时间:2019-02-21 07:12:38

标签: rxjs

我有此代码:

async download(fileToUpload: UploadedFileMetaData): Promise<Observable<DownloadEvent>> {
    const url = await this.getDownloadUrl(fileToUpload);

    let xhr = new XMLHttpRequest();

    xhr.responseType = 'blob';

    return Observable.create((observer) => {
      console.log(observer);
      xhr.open('GET', url);

      xhr.send();

      xhr.addEventListener('progress', (progress) => {
        let percentCompleted;

返回一个ovservable。

然后我像这样使用它:

const downloadSubscription = await this.blobStorageService.download(file);

downloadSubscription.subscribe((event) => // do stuff

可能会创建多个。

我如何退订?

2 个答案:

答案 0 :(得分:0)

您可以将subscribe()方法的返回值Subscriber存储在变量中,并在想要退订时调用unsubscribe()方法。

const downloadSub = downloadSubscription.subscribe((event) => {});

downloadSub.unsubscribe();

答案 1 :(得分:0)

最后我做到了:

public class BatchInvitationDto
{
    public List<Candidates> Candidate { get; set; }
    public string InterviewId { get; set; }
}

public class Candidates
{
    public string Name { get; set; }
    public string Email { get; set; }
}