# uniapp网络请求封装 **Repository Path**: kejit_admin/uniapp-request ## Basic Information - **Project Name**: uniapp网络请求封装 - **Description**: 对原生uniapp网络请求进行了二次封装,减少重复的代码,自带请求超时处理,源代码简洁易于扩展。 详细请看 README 使用时请将导出的方法挂载到全局上,如vue2就挂载到vue的原型上。使用时通过获取当前页面实例来使用该方法。 如果觉得该插件不错请给我一颗星星~ - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2024-07-21 - **Last Updated**: 2024-07-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 网络请求方法使用说明 对原生uniapp网络请求进行了二次封装,减少重复的代码,自带请求超时处理,源代码简洁易于扩展。 使用时请将导出的方法挂载到全局上,如vue2就挂载到vue的原型上。使用时通过获取当前页面实例来使用该方法。 如果觉得该插件不错请给我一颗星星~ **使用实例** 1. 组合式api: ```js const pages = getCurrentPages(); // 获取当前页面实例 let currentPage = pages[pages.length - 1]; let res = await currentPage.$get(url); ``` 2. 选项式apis: ```js let res = await this.$get(url) ``` **初始化:** ```js import {get as $get, post as $post, all as $all, requestMedia as $requestMedia} from "./tools/requestData" // #ifndef VUE3 import Vue from 'vue' import App from './App' Vue.config.productionTip = false Vue.prototype = { ...Vue.prototype, $get, $post, $all, $requestMedia } App.mpType = 'app' const app = new Vue({ ...App }) app.$mount() // #endif // #ifdef VUE3 import { createSSRApp } from 'vue' import App from './App.vue' export function createApp() { const app = createSSRApp(App) app.config.globalProperties = { ...app.config.globalProperties, $get, $post, $all, $requestMedia } return { app } } ``` **方法说明** ```js // 除了第一个参数url是必填其它都是可选。 currentPage.$get(请求url* | 其它参数对象?: { headers, data }) currentPage.$post(请求url* | 其它参数对象?: { headers, data }) currentPage.$all(请求url* | 请求方法?,可以指定其它请求方法,默认是GET | 其它参数对象?: { headers, data }) 本质是get请求,用于获取二进制数据,如音频、图片... currentPage.$requestMedia(请求url* | 其它参数对象?: { headers, data }) ```