Lou*_*nda 2 javascript typescript ionic-framework
试图弄清楚我在声明时错过了什么event.allDay = false。
以下代码工作正常,但 vscode 错误不会消失。
loadFromDevice(year, month, status) {
this.db.dbState().subscribe(res => {
if (res) {
this.db.fetchActivities(year, month, status).subscribe(item => {
// console.log("fetchActivities:")
// console.log(item)
// var entry: {
// allDay: boolean
// }
item.forEach(function (entry) {
var date = new Date();
entry.startTime = new Date(entry.start_datetime);
entry.endTime = new Date(entry.end_datetime);
entry.allDay = false
})
return this.eventSource = item;
})
}
})
}
Run Code Online (Sandbox Code Playgroud)
如果我犯了错误,请感谢任何线索或更正。谢谢你。
解决方案
根据我收到的建议在此处粘贴我的解决方案。
loadFromDevice(year, month, status) {
this.db.dbState().subscribe(res => {
if (res) {
this.db.fetchActivities(year, month, status).subscribe(data => {
// console.log("Activities->loadFromDevice.fetchActivities:")
// console.log(data)
let cal = [];
data.forEach(function(item) {
cal.push({
title: item.title,
startTime: new Date(item.start_datetime),
endTime: new Date(item.end_datetime),
allDay: (item.all_day == 'true') ? true : false
})
})
console.log("Activities->loadFromDevice.cal:")
console.log(cal)
return this.eventSource = cal;
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
这工作得很好!
您不是在编写 JavaScript,而是在编写 TypeScript。
某处(没有提供足够的上下文)有这些entry对象的类型定义,指出该allDay属性由字符串表示。它可能看起来像这样(但也可以是type, class, ... 而不是interface):
interface Entry {
startTime: Date;
endTime: Date;
allDay: string;
}
Run Code Online (Sandbox Code Playgroud)
但是,您正在尝试分配一个布尔false值(不是字符串),因此 TypeScript 编译器会通知您类型冲突。
| 归档时间: |
|
| 查看次数: |
261 次 |
| 最近记录: |