promise axios配合使用

2020-08-07  本文已影响0人  黄黄黄大帅
 checkLogin: () => {
// 返回一个promise对象
    return new Promise((resolve, reject) => {
      axios({
            url: url
            method: 'post',
            data: {
            }
          })
            .then((res) => {
              resolve(res.data);
              // console.log(res);
            })
            .catch(function (error) {
              reject(error);
              // console.log(error);
            });
    });
  }

调用

this.common.checkLogin()
              .then(res => {
                console.log(res);
                  // 执行成功的回调函数
              },
              error => { console.log(error); 
              // 执行失败的回调函数
              });
this.common.checkLogin()
      .then(res => {
             console.log(res);
                  // 执行成功的回调函数
        } )
     .catch(e=>{
        console.log(e)
      })
上一篇 下一篇

猜你喜欢

热点阅读