【问题标题】:how to await a declaration?如何等待声明?
【发布时间】:2020-11-18 04:28:00
【问题描述】:

您好,我尝试为我的 Datatable 设置选项,然后在我的对象中添加一个新字段,但我需要等待这些 dtOptions 完成,我该如何等待 ngOnInit 中的 this.dtoptions

export class MyDashboardComponent implements OnInit {
  grades: any = [];
  dtOptions: DataTables.Settings = {};
  buffer: any = "";

  constructor(private apiService: ApiService, private _auth: AuthService, private http: HttpClient) {
  }

  async ngOnInit(): Promise<void> {
    this.dtOptions = {
      order: [[0, "desc"]],
      responsive: true,
      pagingType: 'full_numbers',
      pageLength: 2,
      serverSide: true,
      processing: true,
      ajax: (dataTablesParameters: any, callback) => {
        this.http
          .post<DataTablesResponse>('http://localhost:4000/grade/readByStudentId', {
            login: this._auth.getUserData().login,
            dataTablesParameters
          }).subscribe(resp => {
          this.grades = resp.data;
          callback({
            recordsTotal: resp.recordsTotal,
            recordsFiltered: resp.recordsFiltered,
            data: []
          });
        });
      },
      columns: [{data: 'id'}, {data: 'firstName'}, {data: 'lastName'}]
    };

    await this.grades.forEach(grade => {
      this.getTpName(grade.tp_id)
        .pipe(take(1))
        .subscribe(tpName => {
          grade.push({tpName : tpName});
        });
    });
  }

【问题讨论】:

    标签: angular typescript async-await


    【解决方案1】:

    将你想要的功能放在 post 功能之后,放在 post 函数的 subscribe() 中。

    将以下代码移动到订阅中您指定成绩值的行之后

    this.grades.forEach(grade => { this.getTpName(grade.tp_id) .pipe(take(1)) .subscribe(tpName => { grade.push({tpName : tpName}); }); });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多