【发布时间】:2020-12-03 22:30:25
【问题描述】:
我想从 typescript 上的 axios 请求中解析数据
我有两个接口
interface Department{
code: string;
name: string;
country: string;
}
interface User {
name: string;
email: string;
departments: Department[];
}
我有一个端点,它返回这样的数据
[
{
"name": "Estonia",
"email": "email",
"phone": 12345,
"weight": "60kg",
"country": "US",
"departments": [
{
"code": 1,
"name": "depto 1",
"country": "US"
},
{
"code": 2,
"name": "depto 2",
"country": "FR"
}
]
[...]
}
]
端点检索到很多我不需要的数据,我只想检索我在接口上定义的属性,这可能吗?
我试过这个,但我得到了所有属性
axios.get<User[]>('http://localhost/users').then(({ data }) => {
console.log(typeof data, data);
});
【问题讨论】:
标签: typescript api parsing axios