最后
四轮技术面+一轮hr面结束,学习到了不少,面试也是一个学习检测自己的过程,面试前大概复习了 一周的时间,把以前的代码看了一下,字节跳动比较注重算法,面试前刷了下leetcode和剑指offer, 也刷了些在牛客网上的面经。大概就说这些了,写代码去了~
祝大家都能收获大厂offer~
篇幅有限,仅展示部分内容
reqErrorFunc: error => Promise.reject(error),
// 响应拦截处理
resHandleFunc: response => response,
resErrorFunc: error => Promise.reject(error)
})
配置参数
除了 axios
提供的默认 请求配置, vue-axios-plugin
也提供了 request/response
拦截器配置,如下:
| 参数名 | 类型 | 默认值 | 描述 |
| — | — | — | — |
| reqHandleFunc
| {Function}
| config => config
| 请求发起前的拦截处理函数 |
| reqErrorFunc
| {Function}
| error => Promise.reject(error)
| 处理请求错误的函数 |
| resHandleFunc
| {Function}
| response => response
| 响应数据处理函数 |
| resErrorFunc
| {Function}
| error => Promise.reject(error)
| 响应错误处理函数 |
示例
–
在 Vue 组件上添加了 $http
属性, 它默认提供 get
和 post
方法,使用如下:
this.$http.get(url, data, options).then((response) => {
console.log(response)
})
this.$http.post(url, data, options).then((response) => {
console.log(response)
})
你也可以通过 this.$axios
来使用 axios
所有的 api 方法,如下:
this.$axios.get(url, data, options).then((response) => {
console.log(response)
})
this.$axios.post(url, data, options).then((response) => {
console.log(response)
})
每个请求方法可以传入三个参数,第一个参数url多接口地址,第二参数是接口请求参数。那么,第三个参数是干什么用的呢?
如果一个项目当中,需要配置多个请求接口的话,该如何做呢?
如果使用vue-axios-plugin
插件的话,会很方便的解决这个问题,这个时候就需要用到请求方法的第三个参数。
首先要明白,第三个参数的类型是对象或者数组(有疑问,自行研究);
例如:
const data = {
phone: ‘renlei’,
code: ‘12’
};
this.$http.get(‘/cardLists’, data, {
interfaceType: ‘first’
}).then(response => {
console.log(response);
});
this.$http.post(‘/login’, data, {
interfaceType: ‘second’
}).then(response => {
console.log(response);
});
这里我分别使用了get和post请求,并且都给其传入了第三个参数。这个第三个参数都是对象,对象里面只有一个键值对,键interfaceType相同,但是值不同。
然后在配置vue-axios-plugin
的文件当中使用传入的第三个参数,例如:
// 请求拦截处理
reqHandleFunc: config => {
console.log(config.interfaceType === ‘first’);
/* config.baseURL =
process.env.NODE_ENV === ‘production’
-
? ‘https://www.520mg.com’
- ‘http://rap2api.taobao.org/app/mock/254896/’; */
let url = ‘http://129.168.1.87/first’;
if (config.interfaceType === ‘first’) {
url = ‘http://129.168.1.87/first’;
} else if (config.interfaceType === ‘second’) {
url = ‘http://129.168.30.85/second’;
}
config.baseURL = url;
config.headers[‘Content-Type’] = ‘application/json’;
config.headers.Accept = ‘application/json’;
config.retry = 4;
config.retryDelay = 1000;
config.timeout = 60000;
return config;
},
如果出现了一种需求是post像get一样使用params传参,那么该如何去做呢?
解决方法:
最后
我可以将最近整理的前端面试题分享出来,其中包含HTML、CSS、JavaScript、服务端与网络、Vue、浏览器、数据结构与算法等等,还在持续整理更新中,希望大家都能找到心仪的工作。
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
篇幅有限,仅展示部分截图: