gpt4 book ai didi

angular - 带有对象的复杂 Typescript 接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 17:32:19 25 4
gpt4 key购买 nike

我有一个接口(interface)定义为:

export interface Address {
addressType: {
house?: {
streetAddress: string,
city: string,
zip: string,
},
apartment?: {
streetAddress: "",
unitNumber: "",
city: "",
zip: "",
buildingName?: "",
},
}
instructions?: string;
}

然后在我的组件的 Typescript 文件中,我定义了一个房屋地址:

address: Address;

constructor() {

this.address.addressType = {
house: {
streetAddress: "1 test",
city: "test city",
zip: "92222",
}
}
console.log(this.address);
}

虽然当我将地址记录到控制台时,我得到:

Uncaught (in promise): TypeError: Cannot set property 'addressType' of undefined

我以为我在构造函数中设置了 addressType。有没有更有效的方法来做我正在做的事情?

最佳答案

你需要初始化this.address才能设置它的addressType:

constructor() {

this.address = {
addressType: {
house: {
streetAddress: "1 test",
city: "test city",
zip: "92222",
}
}
};
console.log(this.address);
}

关于angular - 带有对象的复杂 Typescript 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51833717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com