axios二次封装接口和请求传参

get

1、直接查询

// 封装的接口
import request from "@/utils/request";

export function listHwatchinfo(query) {
  return request({
    url: "/system/hwatchinfo/list",
    method: "get",
    params: query,
  });
}

// 请求
listHwatchinfo().then((response) => {
});

2、根据id查询

// 根据id查询
export function getHwatchtype(id) {
  return request({
    url: "/system/hwatchtype/" + id,
    method: "get",
  });
}

export function getHwatchtype(id) {
  return request({
    url: "/system/hwatchtype/",
    method: "get",
    params: { id },
  });
}

// 请求
getHwatchtype(id).then((response) => {
});



// 当需传多个参数的写法
export function getHistoryInfo(personId, counter) {
  return request({
    method: "get",
    url: "/system/baseinfo/getHistoryInfo",
    params: { personId, counter },
  });
}

export function getHistoryInfo(personId, counter) {
  return request({
    method: "get",
    url: `/system/baseinfo/getHistoryInfo?personId=${personId}&couter=${counter}`,
  });
}

// 请求:需按照接口定义的传参顺序传参
getHistoryInfo(this.userId, this.historyForm.counter).then((res) => {})

// 若直接使用params
export function getHistoryInfo(params) {
  return request({
    method: "get",
    url: "/system/baseinfo/getHistoryInfo",
    params,
  });
}
// 请求
getHistoryInfo({userId:this.userId, counter:this.historyForm.counter}).then((res) => {})

其余请求

1、传参要求为Content-Type:application/json

// 根据类型查询
export function addHwatchtype(data) {
  return request({
    url: "/system/hwatchtype",
    method: "post",
    data,
  });
}

// 请求json方式传参
addHwatchtype(this.form).then((response) => {
});

总结

get请求传参都是params,其余请求都为data

传参要求为JSON时,直接将整个对象传入即可

无传参要求时,用{key:value,key:value}的形式传入;若接口定义了具体传参顺序即直接传对应的value即可

传参要求为form-data时,需要以下处理

// 由于传参格式为fromdata因此需要以下处理
// 数据为对象
let form = new FormData();
form.append("deviceid", deviceid);
form.append("command", command);
submitcommand(form).then();

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二次封装Axios传参方面与原生的 Axios 是类似的。具体来说,可以通过递一个包含请求参数的对象来发送 HTTP 请求。下面是一个示例: ``` import axios from 'axios'; // 创建一个 Axios 实例 const instance = axios.create({ baseURL: 'http://localhost:3000', timeout: 5000 }); // 封装一个 post 请求方法 export function post(url, data) { return instance({ method: 'post', url, data }); } // 发送一个 post 请求 post('/api/user', { id: 12345, name: 'John' }).then(response => { console.log(response); }).catch(error => { console.error(error); }); ``` 在这个示例中,我们首先使用 axios.create() 方法创建了一个 Axios 实例 instance。这个实例包含了一些默认的配置,例如 baseURL 和 timeout。然后,我们封装了一个 post 请求方法,这个方法接收两个参数,一个是请求的 URL,另一个是请求的数据。 在封装的方法中,我们使用 instance() 方法来发送 HTTP 请求。这个方法接收一个对象作为参数,包含了 HTTP 请求的相关配置,例如请求的方法、请求的 URL 和请求的数据。在这个例子中,我们使用了 ES6 的语法,使用了对象的简写方式来递这些配置项。 最后,我们通过调用 post() 方法来发送一个 HTTP POST 请求,并处理响应结果。在这个例子中,我们将请求的 URL 设置为 /api/user,请求的数据为 { id: 12345, name: 'John' }。 总之,在二次封装Axios 中,可以通过递一个包含请求参数的对象来发送 HTTP 请求,并根据需要封装请求方法来简化请求的调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值