axios配置-常见配置项
请求地址:url: '/user'
请求类型:method: 'get'
请根路径:baseURL: 'http://www.mt.com/api'
请求前的数据处理: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() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。