TypeScript接口访问items属性方法 - 如何获取items内容
{"title":"export interface Item {\n\tdesc: string;\n\tid: string;\n\tname: string;\n\tpicture: string;\n\tprice: number;\n}\n\nexport interface GoodsItem {\n\titems: Item[];\n\tcounts: string;\n\tpages: string;\n\tpage: string;\n\tpageSize: string;\n}\n\nexport interface SubType {\n\tid: string;\n\ttitle: string;\n\tgoodsItems: GoodsItem[];\n}\n\nexport interface Result {\n\ttitle: string;\n\tid: string;\n\tbannerPicture: string;\n\tsubTypes: SubType[];\n}\n\nexport interface RootObject {\n\tmsg: string;\n\tresult: Result;\n}定义了类型,但是点不出来items的内容内容:根据提供的代码,可以看出RootObject接口中的result属性是一个Result类型的对象,而Result类型中又包含了一个subTypes属性,是一个SubType类型的数组。\n\n要点出items属性的内容,可以按照以下方式进行访问:\n\ntypescript\n// 假设已经有一个名为rootObj的RootObject对象\nconst items = rootObj.result.subTypes[0].goodsItems[0].items;\nconsole.log(items);\n\n\n这将会打印出items的内容。
原文地址: https://www.cveoy.top/t/topic/p14T 著作权归作者所有。请勿转载和采集!