-3

我有这个异步功能

//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

4

1 回答 1

0

好的,这就是我在做什么

//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'

如您所见,我使用 2 个 args url 和道具,道具将用于获取我需要的令牌

于 2020-05-19T14:32:18.130 回答