[英]TypeScript error in angular: A function whose declared type is neither 'void' nor 'any' must return a value
I'm trying to highlight dates on a calendar.我正在尝试突出显示日历上的日期。 I'm using angular-material-datepicker with dateClass.我将angular-material- datepicker 与 dateClass 一起使用。 The logic is fine but I can't figure out why I'm getting the below error.逻辑很好,但我不知道为什么会出现以下错误。 Can anyone please tell me why I'm getting the below error?谁能告诉我为什么会出现以下错误?
A function whose declared type is neither 'void' nor 'any' must return a value.
HTML: HTML:
<mat-calendar *ngIf="loadCalendar" [dateClass]="dateClass()" [selected]="selectedDate" (selectedChange)="onSelect($event)"></mat-calendar>
Ts: TS:
import { DatePipe } from "@angular/common";
import { Component, OnInit } from "@angular/core";
import { MatCalendarCellCssClasses } from "@angular/material";
import { DataService } from "../app/services/data.service";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"],
providers: [DatePipe],
})
export class AppComponent implements OnInit {
cardData;
dates: string[] = [];
loadCalendar: boolean = false;
constructor(private data: DataService, public datepipe: DatePipe) {}
ngOnInit() {
this.data.getData().subscribe((data) => {
this.cardData = data;
this.cardData.forEach((element) => {
// console.log(this.datepipe.transform(element.start, 'yyyy/MM/dd'));
this.dates.push(element.start);
this.loadCalendar = true;
});
});
}
dateClass() { //Error is caused by this function
return (date: Date): MatCalendarCellCssClasses => {
this.dates.forEach((sD)=>{
let startDate = this.datepipe.transform(sD, "yyyy/MM/dd");
let calendarDate = this.datepipe.transform( date.getDate(),"yyyy/MM/dd");
if (calendarDate === startDate) {
return "special-date";
} else {
return "";
}
})
};
}
}
return (date: Date): MatCalendarCellCssClasses => { <-- !!!You say it returns a MatCalendarCellCssClasses
this.dates.forEach((sD)=>{
let startDate = this.datepipe.transform(sD, "yyyy/MM/dd");
let calendarDate = this.datepipe.transform( date.getDate(),"yyyy/MM/dd");
if (calendarDate === startDate) {
return "special-date";
} else {
return""
}
})
return "" <--- you must also return something here incase forEach does not loop
}
Edit: Updated answer as question was updated编辑:随着问题的更新而更新答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.