在 Angular 17 中,我使用
@for (notification of notifications; track notification) {
<i>{{notification.quotationId}}</i>
}
但在 WebStorm 中我收到警告
未解析的变量quoteId
在 TypeScript 中,属性
notifications
的类型设置为 PendingQuotations
,即:
export type PendingQuotations = {
quotationId: number,
sender: string,
date: string
}[]
为什么我会收到此警告?
这就是我设置属性的方式:
export class HeaderComponent implements OnInit {
public notifications?: PendingQuotations;
constructor(private generalService: GeneralService) { }
ngOnInit(): void {
this.$getNotifications().subscribe((pendingQuotations: PendingQuotations) => {
if (pendingQuotations.length > 0) {
this.notifications = pendingQuotations;
}
});
}
$getNotifications(): Observable<PendingQuotations> {
return this.generalService.get<Response>('/quotation/pending-quotations')
.pipe(
map((res: Response) => res.messages[0] as unknown as PendingQuotations)
);
}
}
请将类型更改为so
export type PendingQuotations = {
quotationId: number,
sender: string,
date: string
}
我不确定你可以像你一样定义类型,所以你可以尝试这个吗!
export class HeaderComponent implements OnInit {
public notifications?: PendingQuotations[];
constructor(private generalService: GeneralService) { }
ngOnInit(): void {
this.$getNotifications().subscribe((pendingQuotations: PendingQuotations[]) => {
if (pendingQuotations.length > 0) {
this.notifications = pendingQuotations;
}
});
}
$getNotifications(): Observable<PendingQuotations[]> {
return this.generalService.get<Response>('/quotation/pending-quotations')
.pipe(
map((res: Response) => res.messages[0] as unknown as PendingQuotations[])
);
}
}