当前位置:网站首页>Uni app third party package configuration network request
Uni app third party package configuration network request
2022-07-06 07:06:00 【Front end childe Jia】
Due to the limitation of the platform , Not supported in applet projects axios, And the original wx.request()
API The function is relatively simple , Global customization functions such as interceptors are not supported . therefore , It is suggested that uni-app Project use @escook/request-miniprogram
Third party packet initiates network data request .
Please refer to @escook/request-miniprogram Official installation documentation 、 To configure 、 Use
Official documents :https://www.npmjs.com/package/@escook/request-miniprogram
1. initialization npm package
npm init -y Initialize at project root package.json file
npm i @escook/request-miniprogram -S Download network request package
2. open main.js
Entrance file
import { $http } from '@escook/request-miniprogram'
// Introduce... From the network request packet $http Request object
uni.$http = $http
// Because it is also used in other pages or components , So will $http The object is attached to the global object , stay uni-app in ,uni Global object
$http.baseUrl = 'https://www.baidu.com'
// Configure network request base address , because uni.$http by $http References to , So you can modify it directly $http Properties under the object
$http.beforeRequest = function() {
// Configure the interception function before initiating the network request
uni.showLoading({
title: ' Loading ...'
})
}
$http.afterRequest = function() {
// Configure the interception function after the network request
uni.hideLoading()
}
// Encapsulation request failure prompt
uni.$showMsg = function(title=' Data load failed ',duration=1500){
uni.showToast({
title,
duration,
icon:'none'
})
}
3. Initiate network request
async getSwiperList() {
const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata')
if (res.meta.status !== 200) return uni.$showMsg()
this.swiperList = res.message
}
边栏推荐
- Hydra common commands
- UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
- leetcode59. 螺旋矩阵 II(中等)
- First knowledge of OpenGL es learning (1)
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- 开源的网易云音乐API项目都是怎么实现的?
- 微信脑力比拼答题小程序_支持流量主带最新题库文件
- What is the biggest problem that fresh e-commerce is difficult to do now
- 巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
- Proteus -- Serial Communication parity flag mode
猜你喜欢
AI on the cloud makes earth science research easier
Depth residual network
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
Leetcode59. spiral matrix II (medium)
Thought map of data warehouse construction
顶测分享:想转行,这些问题一定要考虑清楚!
树莓派3B更新vim
leetcode841. Keys and rooms (medium)
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
随机推荐
ROS学习_基础
RichView TRVStyle 模板样式的设置与使用
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
What does UDP attack mean? UDP attack prevention measures
BUU的MISC(不定时更新)
1189. Maximum number of "balloons"
Simple use of JWT
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
3. Business and load balancing of high architecture
配置树莓派接入网络
UWA pipeline version 2.2.1 update instructions
CDN acceleration and cracking anti-theft chain function
You deserve this high-value open-source third-party Netease cloud music player
Top test sharing: if you want to change careers, you must consider these issues clearly!
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
Entity Developer数据库应用程序的开发
leetcode6109. 知道秘密的人数(中等,周赛)