天啦,整了一天的上傳導入,小菜雞都要崩潰了,看了一波源碼實現機制,似乎明白了一些,但是實際操作慘不忍睹,所以要記錄一下我的騷操作,以后遇到要導入上傳免得忘了
首先就是要引入上傳組件啦
import { Upload } from 'hzero-ui';
然后直接使用,因為上傳是一個點擊按鈕的操作,自然要綁定一個button
<Upload {...uploadProps}>
<Button>
<Icon type="upload" /> 上傳
</Button>
</Upload>
然后給一個props
const uploadProps = { name: 'file', action: `接口`, data: { 參數 }, headers: { authorization: `授權token`, }, showUploadList: false, // 不展示上傳內容 onChange(info) { if (info.file.status !== 'uploading') { console.log('ffffffff', info.file, info.fileList); } if (info.file.status === 'done') { message.success(`${info.file.name} file uploaded successfully`); } else if (info.file.status === 'error') { message.error(`${info.file.name} file upload failed.`); } }, };
在這里面我被困了兩點:
第一點就是我寫action的接口的時候,他自己去調用的是本地接口,可把我整懵了,平時都不是這樣的,沒辦法了就是能手動加個apihost,但是這樣就迎來了第二點,沒有授權token,於是獲取授權token
import { getAccessToken } from 'hzero-front/lib/utils/utils';
const accessToken = getAccessToken();
解決
