我分享了一段代碼,證明我有兩種型別的response
. 如果您查看下面的代碼,您會發現response
服務結果會略有不同。在第一種型別中,您有一個,data
但在第二種型別中,它本身data
就有另一個data
。
{
"data": {
"hasError": false,
"developerMessage": null,
"totalCount": 43
},
"hasError": false,
"developerMessage": null
}
或者:
{
"data": {
"hasError": false,
"developerMessage": null,
"data": {
"hasError": false,
"developerMessage": null,
"totalCount": 43
},
"totalCount": 43
},
"hasError": false,
"developerMessage": null
}
所以為了處理這個困境,我必須首先檢查結果response
并將其分成兩個塊。最大的問題是,由于我無法在這里解釋的原因,這個層次結構可能會擴展并且我有三個資料,但我們可以肯定的是data
inresponse
總是最深的。(本例中的第三項)
return this.httpWrapperService.get(url tableNameWithSlash, {
params: params, additionalOptions: {spinnerEnabled: false}
}).then((result: any) => {
if (result.data.data) {
// code
return [...result.data.data];
}
else if (result.data) {
// code
return [...result.data];
}
});
我想知道是否有更好的解決方案來處理這種動態response
.
uj5u.com熱心網友回復:
您可以使用遞回來制作適用于任何深度的解決方案,例如資料位于第 4 層
.then((result: any) => {
const findData = (response: any) => {
return response.data ? findData(response.data):response;
}
return [...findData(result.data)];
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497757.html
標籤:javascript 有角度的 打字稿