当前位置:网站首页>Simple encapsulation steps of request data request of uniapp
Simple encapsulation steps of request data request of uniapp
2022-07-27 14:18:00 【A bone preference】
uniapp Of request Simple encapsulation steps for data requests
1. New file 、 newly build api.js
// Encapsulation request
const BASE_URL = 'https://api-hmugo-web.itheima.net/api/public/v1'; // Root address
export const myRequest = (options)=>{
return new Promise((resolve,reject)=>{
uni.request({
url:BASE_URL + options.url,
method:options.method,
data:options.data || {
},
success: (res) => {
// When getting failed
// console.log(res)
if(res.data.meta.status != 200){
return uni.showToast({
title:' Get data is empty ',
icon:"error",
duration:1600,
})
}
// On success
resolve(res)
},
fail: (err)=>{
uni.showToast({
title:' Request interface failed '
})
reject(err)
}
})
})
// Request data Example Use encapsulation in future pages
// myRequest({
// url:'',
// method:'POST',
// data:{
}
// })
}
2. Need to be in main.js File and install 
import {
myRequest} from '@/until/api.js'
// install
Vue.prototype.$myRequest = myRequest
3. Call encapsulation in the page **( stay onload To load the requested data )**
// Load data before request
onLoad() {
this.getSwipers()
},
methods: {
async getSwipers(){
const res = await this.$myRequest({
url:'/home/swiperdata'
})
console.log(res.data.message)
this.swipers = res.data.message // Transfer the obtained data into data in
}
}
}
4. Data display effect 
notes : Just encapsulate the data request , Interceptors are not designed : Request to intercept 、 The response to intercept
边栏推荐
- 在灯塔工厂点亮5G,宁德时代抢先探路中国智造
- va_list 使用总结
- 认知篇----硬件工程师的成才之路之经典
- What are the benefits of taking NPDP
- DVWA全级别通关教程
- Flexible and easy to use WYSIWYG visual report
- Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers
- Leetcode · daily question · 592. fraction addition and subtraction · simulation
- 万字详解 Google Play 上架应用标准包格式 AAB
- 第3章业务功能开发(查看线索明细)
猜你喜欢

VSCode -- 创建模板文件

Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020

面向不平衡数据的电子病历自动分类研究

10 practical uses of NFT

this指向问题,闭包以及递归

【idea】设置提取serialVersionUID

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

知识关联视角下金融证券知识图谱构建与相关股票发现

【多线程的相关内容】

Good architecture is evolved, not designed
随机推荐
Interview secrets are widely distributed, and the exclusive secrets of editing, testing and learning are leaked?!
Document translation__ Tvreg V2: variational imaging method for denoising, deconvolution, repair and segmentation (part)
Some key information about Max animation (shift+v)
机场云商sign解析
GoPro接入 - 根据GoPro官方文档/Demo,实现对GoPro的控制和预览
C#测量工具示意图
Redis implements the browsing history module
为什么会出现Script file ‘D:\Anaconda3\envs\paddle_env\Scripts\pip-script.py‘ is not present.
解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
RTL8762DK 环境搭建(一)
@Repository详解
关于max做动画的一些关键信息(shift+v)
HDU1422 重温世界杯【DP】
windows10 安装Sql Server 2019
Docker实践经验:Docker 上部署 mysql8 主从复制
Arduino+ze08-ch2o formaldehyde module, output formaldehyde content
开源版思源怎么私有部署
致尚科技IPO过会:年营收6亿 应收账款账面价值2.7亿
VSCode -- 创建模板文件
A Keypoint-based Global Association Network for Lane Detection