当前位置:   article > 正文

axios异步请求的两种写法_axios 异步写法

axios 异步写法

1、async 、await写法

  1. async function sendRequest(type:string,url:string,param?:any,tableData?:Ref) {
  2. try {
  3. const response = await axios({
  4. url: `${baseUrl}${url}`,
  5. method: type, //请求方式:get、post等
  6. params: param,
  7. paramsSerializer: params => {
  8. return stringify(params,{indices:false,allowDots:true});
  9. }
  10. });
  11. if(undefined !== tableData){
  12. tableData.value = response.data;
  13. }
  14. // return response.data;
  15. } catch (error) {
  16. ElMessage(String(error));
  17. }
  18. }

2、promise对象的.then   .catch写法

  1. /**
  2. * type:指定访问类型,post,get,……
  3. * url:传入访问地址
  4. * param:访问参数,可选
  5. * tableData:传入的展示数据响应对象,可选
  6. */
  7. function sendRequest(type: string, url: string, param?: any, tableData?: Ref) {
  8. axios({
  9. url: `${baseUrl}${url}`,
  10. method: type,
  11. params: param,
  12. paramsSerializer: params => {
  13. return stringify(params, { indices: false, allowDots: true });
  14. }
  15. }).then((response) => {
  16. if (undefined !== tableData) {
  17. tableData.value = response.data;
  18. }
  19. }).catch(err=>{
  20. ElMessage(String(err));
  21. })
  22. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/467937
推荐阅读
相关标签
  

闽ICP备14008679号