使用自己的参数处理请求错误

Handle a request error with its own paramaters

我在 Web 应用程序中使用 axios 发出了 GET 请求:

axios({
            method: 'get',
            url: "example.com",              
            params:{
                    _id: "anId"
                }

        })
            .then((response) => {
                //do stuffs
            })
            .catch((error) => {
                // need params._id here ?!!
            })

是否可以在请求的错误处理部分获取参数? 谢谢

您可以关闭(关闭)参数以使它们保持在上下文中:

function myWrapper() {
  const params = {
    a: 'a',
    b: 'b'
  }
  axios({
      method: 'get',
      url: "example.com",
      params: params

    })
    .then((response) => {
      // you have access to params here
      console.log(params.a);
      //do stuffs
    })
    .catch((error) => {
      // you have access to params here
      console.log(params.a);
      // need params._id here ?!!
    })
}

注意,这没有经过测试,但应该可以完成

这是一个 运行 示例:

function myWrapper() {
  const params = {
    a: 'a',
    b: 'b'
  }
  axios({
      method: 'get',
      url: "example.com",
      params: params

    })
    .then((response) => {
      // you have access to params here
      console.log(params.a);
      //do stuffs
    })
    .catch((error) => {
      // you have access to params here
      console.log('error', error.message);
      console.log('params!!', params);
      // need params._id here ?!!
    })
}

myWrapper();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>