javascript - 初始化对象的类型不存在属性
问题描述
我将数据分配给接口对象。
我用可选字段创建了我的接口
export interface IWeek {
type?: number;
required?: string;
}
export interface IItem {
code?: string;
recipe?: IRecipes;
}
export interface IMenu {
week?: IWeek;
items?: IItem[];
title?: string;
}
然后我将数据分配给对象
const data :IMenu ={} ;
data.week.type=date;
data.title=title;
recipes.map(elm => {
data.items.push({code:code,recipe:elm})
})
我收到错误消息,类型对象上不存在项目
ERROR in src/app/validation-dialog/validation-dialog.service.ts(13,10): error TS2339: Property 'week' does not exist on type '{}'.
src/app/validation-dialog/validation-dialog.service.ts(14,10): error TS2339: Property 'title' does not exist on type '{}'.
src/app/validation-dialog/validation-dialog.service.ts(16,12): error TS2339: Property 'items' does not exist on type '{}'.
解决方案
您尚未定义属性items
。首先将其初始化为一个空数组。
const data: IMenu = {};
data.items = [];
推荐阅读
- php - 如何使用 Doctrine 组合 Symfony 4 中唯一的两列?
- firebase - 在 null 上调用了方法“add”
- js-xlsx - 在使用带有 XLSX.utils.json_to_sheet 的标头选项时,标头不会覆盖
- php - 如何避免插入重复记录
- vba - 在 VB 中简化多个类似的“私人潜艇”
- javascript - Chrome 任务管理器中的内存占用、Javascript 内存和实时内存有什么区别?
- java - 如何停止textview的褪色动画?
- python - Django 是 MVC 还是 MVT 框架?
- jquery - Ajax 调用后关闭引导模式
- azure - Azure 函数 V2 可以缓冲日志调用吗?