nlrx-wjc / ts-axios Goto Github PK
View Code? Open in Web Editor NEW使用TypeScript重构axios系列文章https://www.cnblogs.com/wangjiachen666/category/1510064.html
使用TypeScript重构axios系列文章https://www.cnblogs.com/wangjiachen666/category/1510064.html
感谢博主的分享,系列文章受益匪浅。
关于第二篇文章处理get请求url参数2.7似乎有一些问题。
我使用axios 0.21时,当请求的url含有hash时,axios的处理是:将哈希值后面的参数全部舍去,但保留了传入的参数。
ex:
axios({
method: 'get',
url: '/api/handleRequestURL/get#hash?bar=baz',
params: {
foo: 'bar'
}
})
实际请求的地址为:/api/handleRequestURL/get?foo=bar
,而非/api/handleRequestURL/get
。也就是说,如果config中带有参数,参数最后还是会拼接到url上的。
这是axios源码中提供的部分代码:
// buildURL.js
module.exports = function buildURL(url, params, paramsSerializer) {
// ...
if (serializedParams) {
var hashmarkIndex = url.indexOf('#');
if (hashmarkIndex !== -1) {
url = url.slice(0, hashmarkIndex);
}
url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
}
return url;
}
我觉得,可以将buildURL.js
中的对含有hash的处理进行简单的修改
// buildURL.js
// 如果url中有哈希标记,则直接返回原始url
if (url.includes('#')) {
const markIndex = url.indexOf('#')
url = url.slice(0, markIndex)
// TODO: modify
// return url
return buildURL(url, params)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.