我是新手,我正在嘗試做簡單的事情。
我從 BE 接收 JSON 資料,如下所示:
{"data":[{"id":"12345",
"name":"Blabla",
"otherName":"3300",
"completeDate":"2021-10-01T05:00:00.000 0000",
"status":"Active",
"location":"572957393.pdf"}]}
現在位置是檔案存盤在服務器上的位置,我需要將 data.location 與 URL 連接起來,所以當我渲染它時,而不是這個,"572957393.pdf"我會看到這個"https://www.mypage.com/files/https572957393.pdf"
我認為最好的方法是在 fetch class-method 中使用 .map 方法,但我不能讓它作業
獲取語??句:
fetchData(params, id){
axios({
url:`/admin/${this.state.radioSelect}/detail`,
method:'get',
params
}).then(resp => {
//const updated = resp.data.map(location) => This is where I am trying to use .map method
.then(resp => {
if(resp.data.code == '200'){
this.setState({
pageSize:this.state.pagination.pageSize,
records:resp.data.data,
recordsTableSpin:false,
recordsId:id,
})
}
})
}
你能幫我解決這個問題嗎?謝謝。
uj5u.com熱心網友回復:
如果我正確理解了您的問題,我認為您可以執行以下操作:
fetchData(params, id){
axios({
url:`/admin/${this.state.radioSelect}/detail`,
method:'get',
params
}).then(resp => {
if(resp.data.code == '200'){
const updatedData = resp.data.data.map(element => ({
...element,
location: element.location resp.data.location
}))
this.setState({
pageSize: this.state.pagination.pageSize,
records: updatedData,
recordsTableSpin: false,
recordsId: id
})
}
})
}
uj5u.com熱心網友回復:
連接要在瀏覽器中顯示檔案的 URL。您可以使用兩種不同的狀態(一種用于檔案名,一種用于 URL),以便您可以像使用模板文字一樣連接它們
${URL}${fileName}
或者
URL fineName
試試這個來使用 MAP
const temp = data.map((ele) => { return { ...ele, file: url fileName }; });
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318224.html
標籤:javascript 反应 网址 获取 API
上一篇:無需重新加載的頁面切換
