打字稿中的错误〜属性在void类型中不存在〜

时间:2020-01-19 01:06:15

标签: html angular typescript date void

好吧,碰巧我有一些用角度引导程序制作的代码,它是一个数据选择器,它位于事件方寄存器(您单击new并创建一个新事件)中,并且我希望用户选择日期和保存它,下面是日期选择器的代码:

  <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;

  })
}

任何人都可以告诉我原因吗?

1 个答案:

答案 0 :(得分:0)

TL; DR(简短答案):

您的方法salvarImagem(): void不返回任何内容(显式datahoje),并且在代码上查看时,您应该返回一个具有属性salvarImagem(): { datahoje: Date } { ... return { datahoje }; } 的对象

更长的答案:

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

this.selecionado['dataInicial']

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

async isAuthenticated() {
        new Promise (
            (resolve, reject) =>  {
                setTimeout(() => {
                    resolve(this.loggedIn);
                }, 800);
            }
        );
    }

有关Typescripts类型的一点:

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

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