Dav*_*ung 6 deserialization typescript axios typescript2.0
我有以下axios获取请求:
export function getCurrentCart() {
return axios.get(Constants.ENDPOINT_MENU_TIMESLOTS, getConfig())
.then(res => {
let data = (res as AxiosResponse).data;
if (data != null) {
// parsing logic to singleton CartStore instance here
...
} else {
console.error("data is null");
}
});
}
Run Code Online (Sandbox Code Playgroud)
它检索一个 tsCartStore类:
export class CartStore {
id: number;
temporaryTotal: number;
status: string = "SHOPPING";
statusLockTime?: any;
updatedAt?: string;
createdAt?: string;
coupon?: Coupon;
readonly items = observable.shallowArray<CartItem>([]); // this is a mobx observable
@computed get hasItem(): boolean {
return this.items.length > 0;
}
@action clear() {
this.items.clear();
}
}
Run Code Online (Sandbox Code Playgroud)
哪里Coupon和CartItem是其他模型类。
这是示例 JSON 响应:
{
"createdAt": "2017-02-16T20:37:13",
"updatedAt": "2017-02-22T22:48:50",
"id": 1,
"status": "SHOPPING",
"statusLockTime": null,
"temporaryTotal": 0,
"items": [
... array of some other json object
],
"coupon": {
... json object
}
}
Run Code Online (Sandbox Code Playgroud)
你们推荐哪个解串器库?另一个问题是:如何在不使用任何库的情况下以最TS的方式做到这一点?
| 归档时间: |
|
| 查看次数: |
1836 次 |
| 最近记录: |