【发布时间】:2019-03-18 23:46:53
【问题描述】:
我的后端有这个 JSON 响应:
//User_Courses
[
{
id: 1,
name: "Ice King",
email: "pretty_princess1234@gmail.com"
completedCourses: [1,3],
unlockedCourses: [1,3,4,5,6],
completedLessons: [{"1" => [1,2,3]}, {"3" => [1,2,3,4,5,6,7]}, {"4" => [1]}]
},
{
id: 2,
name: "Mr. Crocker",
email: "fairy_godparents111@gmail.com"
completedCourses: [3],
unlockedCourses: [1,3,4],
completedLessons: [{"3" => [1,2,3,4,5,6,7]}, {"4" => [1,2]}]
}
]
// completed lessons are all the lesson the user finished.
// courses can be in progress or completed.
我想从后端获取数据并订阅这个接口。 我不确定如何实现数据结构以及如何访问数据。 这是我创建的界面:
export interface IUser {
id: number;
name: string;
email: string;
completedCourses: number[];
unlockedCourses: number[];
completedLessons: // <----- don't know what type to write
}
我想知道如何实现这一点,使用服务订阅数据和访问数据(以便以后更改并添加数据)。 非常感谢!
【问题讨论】:
-
completedLessons: any[]听起来很合适。 -
以及如何订阅和访问数据?
-
你可以使用这种类型的数组:
interface CompletedLesson{[name:string]:number[]} -
好的,听起来不错。那我怎么订阅呢?我需要像往常一样做一些特别的事情或订阅吗?
-
而且 CompletedLesson 是数组吗?我可以通过添加 [] 使其成为数组吗?
标签: json angular typescript interface hashmap