我正在尝试通过调用api来创建一个模型,该模型返回响应中的对象。
我有一个简单的class
,但现在模型中有一个对象,我不知道如何创建我的class
作为未定义的school->name
属性我的页面和打字稿正在抱怨。
export class User {
title: string;
first_name: string;
last_name: string;
school : object {
name: string
}
}
添加school
对象
谢谢
答案 0 :(得分:1)
你很亲密 - 你只是忽略了“对象”这个词。像这样:
export class User {
title: string;
first_name: string;
last_name: string;
school: {
name: string
}
}
// Sample usage
const user = new User();
user.school = { name: 'Beacon Hills High School' }
您还可以使用对象文字创建用户:
const user: User = {
title: 'Mr',
first_name: 'Student',
last_name: 'Rik',
school: { name: 'Beacon Hills High School' }
};
答案 1 :(得分:1)
尝试这种正确的模型创建方式
export class User {
title: string;
first_name: string;
last_name: string;
school: School;
}
export class School {
name: string;
}
let user = new User();
let school = new School();
school.name = 'Beacon Hills High School';
user.school = school;