我有这个异步函数
//example get
async getDatafromAPINODE(url) {
try {
let respond = await axios.get(API_URL_NODE + url, {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + this.state.tokenUser,
},
});
if (respond.status >= 200 && respond.status < 300) {
console.log("respond Post Data", respond);
}
return respond;
} catch (err) {
let respond = err;
console.log("respond Post Data err", err);
return respond;
}
}
如何使此功能可导出? 所以我可以在另一个文件中使用import
好吧,我在做什么
//asyncFunction.js
export const getDatafromAPINODE = async (url, props) => {
try {
let respond = await axios.get(API_URL_NODE + url, {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + props,
},
});
if (respond.status >= 200 && respond.status < 300) {
console.log("respond Post Data", respond);
}
return respond;
} catch (err) {
let respond = err;
console.log("respond Post Data err", err);
return respond;
}
};
然后我打电话用
import {getDatafromAPINODE} from '../../../helper/asyncFunction'
如您所见,IM 使用 2 个 args URL 和道具,这些道具将用于获取我需要的令牌