首页 > 解决方案 > 打字稿中的错误〜属性在类型void中不存在〜

问题描述

好吧,碰巧我有一些用 angular bootstrap 编写的代码,它是一个数据选择器,它位于事件方寄存器内(您单击新建并创建一个新事件),我希望用户选择一个日期并保存它,这是日期选择器的代码:

  <p>Selecione a data</p>

                    <ngb-datepicker #dp [startDate]="modelAtual" [(ngModel)]="modelAtual" (navigate)="date = $event.next"></ngb-datepicker>

                            <pre>Month: {{ date.month }}.{{ date.year }}</pre>
                            <pre>Model: {{ model | json }}</pre>

然后我有代码来保存日期的信息,它在打字稿中:

  salvarImagem() {
    if (this.chave === undefined) {
      let dat1=new Date().getMilliseconds();
      const filePath = `/eventos/${dat1}-${this.selecionado["lastModified"]}` + '.' + this.selecionado['tipoarquivo'];
      const fileRef = this.storage.ref(filePath);
      const task = this.storage.upload(filePath, this.file);
      let datahoje = new Date(this.modelAtual.year,this.modelAtual.month,this.modelAtual.day).getTime();
      this.selecionado['dataInicial']=datahoje;
      var horario = this.time.hour + ':'+ this.time.minute;
      this.selecionado['horaInicial']=horario; 



      // observe percentage changes
      this.uploadPercent = task.percentageChanges();
      // get notified when the download URL is available
      task.snapshotChanges().pipe(
        finalize(() => {
          this.downloadURL = fileRef.getDownloadURL();
          this.downloadURL.toPromise().then(du => {

            this.selecionado["arquivo"] = du;

            this.service.add(this.selecionado);

            this.router.navigate(['/eventos']);
            alert("Objeto salvo");

          });
        })
      ).subscribe();

    } else {
      this.service.update(this.chave, this.selecionado);
      this.router.navigate(['/estabelecimentos']);
      alert("Objeto salvo");
    }
  }

然后我有当用户想要编辑这个寄存器时加载的代码(你可以这样做)并且为了准确显示他之前选择的日期,我尝试将变量 modelAtual 分配给变量 datahoje,出于某种原因返回标题中提到的错误

if (this.chave !== undefined) {
  this.service.get(this.chave).valueChanges().subscribe(obj => {
    this.selecionado = obj;
    this.logo = this.selecionado["arquivo"];
    //this.modelAtual = { year: 2011, month: 7, day:23};
    this.modelAtual = this.salvarImagem().datahoje;

  })
}

有人能告诉我原因吗?

标签: htmlangulartypescriptdatevoid

解决方案


TL;DR(简短回答):

您的方法salvarImagem不返回任何内容(显式 salvarImagem(): void)并查看您的代码,您应该返回一个具有该属性的对象datahoje

更长的答案:

为避免错误,您需要返回值

salvarImagem(): { datahoje: Date } {
  ...
  return { datahoje };
}

或调用该方法并从您编辑的对象中获取值(作为副作用)

this.selecionado['dataInicial']

关于 Typescripts 的类型:

https://www.typescriptlang.org/docs/handbook/basic-types.html

此外,您的 IDE“知道”返回类型- 您可以通过 ctrl(MacOS 的 cmd) + 将鼠标悬停在该方法上来检查它。


推荐阅读