如何使这个函数更小?

我想通过删除发送到 htttp.post 的变量并将它们放入数组来优化以下函数。这可能吗?


将变量放在普通数组中,但出现布尔错误。


  getUsers(val) {

    this.spinner.show();

    const options = {

      headers: new HttpHeaders().set('x-access-token', this.currentUser.token),

      responseType: 'blob' as 'json'

    };

    return this.http.post(this.apiRoot + val, { status: this.userAdvanceSearch.status, email: this.userAdvanceSearch.email, filterWithin: this.filterQuery, name: this.userAdvanceSearch.name, userTypeNo: this.authenticationService.getCurrentUserType().userTypeNo }, options).subscribe((res: any) => {

      val === 'pdf' ? this.blob = new Blob([res], { type: 'application/pdf' }) : this.blob = new Blob([res], { type: 'text' });

      const downloadURL = window.URL.createObjectURL(res);

      const link = document.createElement('a');

      link.href = downloadURL;

      val === 'pdf' ? link.download = 'users.pdf' : link.download = 'users.csv';

      link.click();

      window.URL.revokeObjectURL(downloadURL);

      this.spinner.hide();

    });

  }

这按原样工作,但如果可能的话,我希望它更干净、更短。


PIPIONE
浏览 133回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript