html - 打字稿中的错误〜属性在类型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;
})
}
有人能告诉我原因吗?
解决方案
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) + 将鼠标悬停在该方法上来检查它。
推荐阅读
- xamarin - 在 Android Emulator 中运行的应用程序无法向 localhost 执行 HTTP Post
- reporting-services - 如何挂钩到 SSRS 中的两个数据集?
- python - 需要代码:使用6个不同的高斯分类器对IRIS数据集进行分类精度
- php - 在php中显示数组列表中的所有记录
- uwp - 据报道 uwp 应用程序在启动时崩溃,但在我的计算机上运行良好
- python-3.x - 通过行解析并从 Spark Dataframe 中隔离学生记录
- python - 截断二进制数的最佳方法
- angular - 反应式表单数组 - 推送新元素时避免验证错误
- java - Android Base64 图片上传进度
- javascript - 如何在 HTML5 中将日期输入连接到图像输出