2023-3-26学习笔记

文章详细介绍了Axios库的常见配置项,包括请求地址、类型、基础URL、数据处理、请求头、查询参数等。同时,提到了Object.keys()、Array.prototype.forEach()、filter()和map()等JavaScript数组方法的用途和示例。
摘要由CSDN通过智能技术生成

axios配置-常见配置项

  • 请求地址:url: '/user'

  • 请求类型:method: 'get'

  • 请求前的数据处理:transformRequest:[function(data){}]

  • 请求后的数据处理: transformResponse: [function(data){}]

  • 自定义的请求头:headers:{'x-Requested-With':'XMLHttpRequest'}

  • URL查询对象:params:{ id: 12 },

  • 查询对象序列化函数:paramsSerializer: function(params){ }

  • request body:data: { key: 'aa'}

  • 超时设置:timeout: 1000,

  • 跨域是否带Token:withCredentials: false

  • 自定义请求处理:adapter: function(resolve, reject, config){}

  • 身份验证信息:auth: { uname: '', pwd: '12'}

  • 响应的数据格式json / blob /document /arraybuffer / text / stream:responseType: 'json'

请求方式别名

为了方便起见,已经为所有支持的请求方法提供了别名。

axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
axios(config)
// 发起一个post请求axios({
  method:'post',
  url:'/user/12345',
  data:{
    firstName:'Fred',
    lastName:'Flintstone'}});

Object.keys()

Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和正常循环遍历该对象时返回的顺序一致。

Array.prototype.forEach()

forEach() 方法对数组的每个元素执行一次给定的函数。

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

Array.prototype.filter()

filter() 方法创建给定数组一部分的浅拷贝,其包含通过所提供函数实现的测试的所有元素。

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// Expected output: Array ["exuberant", "destruction", "present"]

Array.prototype.map()

map() 方法创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。

const array1 = [1, 4, 9, 16];

// Pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// Expected output: Array [2, 8, 18, 32]

Function.prototype.bind()

bind() 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值