从 Axios 中的响应映射字段?
Mapping fields from response in Axios?
我有以下代码:
axios.get<Response>(url)
.then(response => {
console.log(response.data);
}).catch(error => {
// handle error
console.log(error);
});
JSON 响应包含我无法在 TypeScript 中表达的字段:
{
"field:Type": "foo"
}
喜欢:name:Type
包含 string
- 在我的例子中是 "foo"
如何在 Response
界面中映射这样的字段以便以后使用?
interface Response {
myMappedField: string;
}
在打字稿中你必须用单引号把它包起来,
interface Response {
myMappedField?: string;
'field:Type'?: string;
}
在 axios 响应对象中,您可以使用 Dot/Bracket 符号来读取 属性。
response.data['field:Type']
我有以下代码:
axios.get<Response>(url)
.then(response => {
console.log(response.data);
}).catch(error => {
// handle error
console.log(error);
});
JSON 响应包含我无法在 TypeScript 中表达的字段:
{
"field:Type": "foo"
}
喜欢:name:Type
包含 string
- 在我的例子中是 "foo"
如何在 Response
界面中映射这样的字段以便以后使用?
interface Response {
myMappedField: string;
}
在打字稿中你必须用单引号把它包起来,
interface Response {
myMappedField?: string;
'field:Type'?: string;
}
在 axios 响应对象中,您可以使用 Dot/Bracket 符号来读取 属性。
response.data['field:Type']