ITPub博客

首页 > IT职业 > IT图书 > vue 使用 axios

vue 使用 axios

IT图书 作者:胡扯 时间:2021-09-09 10:34:50 0 删除 编辑

vue 中使用axios 的方式

第一种

定义axios函数

a 文件

import axios from "axios";
export default axios.create({    
    baseURL:'接口',    
    // headers:{'content-type':'application/json'}    
    // application/x-www-form-urlencoded
 })

b 文件调用

import axios from "./axios";
   
  
export const colum  = (obj)=>{    
    return axios.post('子接口',{
        参数
    }) 
}

vue 文件 处理函数返回参数

 colum({ 参数 }).then(res => {
           处理返回数据
      });


第二种

定义函数

pub.js

 import $ from "jquery";
 import axios from 'axios'
 
 _InitAxios(op) {       
  axios({                
      url: op._url + op.ur,                
      method: "post",                
      data: JSON.stringify(op.data),               
      headers: {                    
           "Content-Type": "application/json"               
        }            
      })            
      .then(res => {                
            console.log('axios 访问返回数据', res)               
             op.cbk(res.data);           
      })           
      .catch(cat => {});   
   }

vue 文件调用

import $ from "jquery";
import axios from "axios";
import { pub } from "./assets/js/pub";
 var op_data = {        
     that: that,        
     _url: ,// 总接口        
     ur: , // 具体接口        
     cbk: that.cb, // 回调        
     data: { } // 形参      
 };      
  pub._InitAxios(op_data);
  cb(res){
      处理返回参数
  }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/166/viewspace-2823760/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇:
下一篇: 继承的收获
请登录后发表评论 登录
全部评论

注册时间:2019-04-11

  • 博文量
    0
  • 访问量
    500