我这个是taro3+vue3加ts的项目,不过taro封装请求是一样的
taro中有封装好的请求,不需要像vue引入axios
在src下新建一个文件夹未network名字可以随便取
![](https://img-blog.csdnimg.cn/img_convert/cbfc47f730d54179c6496e6b8bb05117.png)
在network中新建baseUrl.ts里面存的是生产环境与开发环境的url开头,我是在微信调试工具做的,所以暂时没有用到
![](https://img-blog.csdnimg.cn/img_convert/35931fbc09624a4ba794190746f9b54c.png)
新建config.ts存放返回的状态码
![](https://img-blog.csdnimg.cn/img_convert/5e682e77b449ed904594938511012c4c.png)
新建interceptors.ts里面是拦截器
![](https://img-blog.csdnimg.cn/img_convert/c817b3d319807be8c8d5fa557689bfb5.png)
新建utils.ts获取当前页的url
![](https://img-blog.csdnimg.cn/img_convert/ae74f325de2b695b2de086a48c1a19c6.png)
封装request请求,baseUrl这里是我请求后台的端口号
![](https://img-blog.csdnimg.cn/img_convert/bdff027bd4efe68182389ae4fe448eb0.png)
在pages下的api中封装一下request请求,这样子使用的时候很方便
![](https://img-blog.csdnimg.cn/img_convert/a196c342c62273f3a3a0d4afd42a28f5.png)
示例
![](https://img-blog.csdnimg.cn/img_convert/ce5844d599462c61c935f1cfb935f2e2.png)
这样子就差不多了,第一次写博客有什么不好的地方希望理解一下,谢谢!