当前位置:网站首页>使用uniapp 封装一个request 请求
使用uniapp 封装一个request 请求
2022-08-03 16:08:00 【要成为光的女人】
1.定义一个baseURL 用来拼接地址
2.封装一个request 请求
// 定义一个baseURL用来拼接地址
const BASE_URL="https://shop.helpetmall.com/api/";
//封装一个request请求
export const myRequest=(options)=>{ //传入的options是一个json对象
return new Promise((resolve,reject)=>{
uni.request({
url:BASE_URL+options.url,
method:options.methods||"POST",//请求方式,设为POST
data:options.data || {},//前台传入后台的数据
dataType:options.dataType || "json",
success: (res) => {
// if(res.data.status !== 0){
// return uni.showToast({
// title:"获取数据失败"
// })
// }
resolve(res)
},
fail: (err) => {
// uni.showToast({
// title:"接口请求失败"
// })
// reject(err)
}
})
})
}3.在main.js下引用和全局设置(挂载)
import Vue from 'vue'
import App from './App'
//全局引入vuex
import store from './store/index.js';
//全局引入封装好myRequest的接口请求
import { myRequest } from "./util/Api.js";
import {intoGoods} from './util/intoGoods.js';
Vue.prototype.$myRequest = myRequest; 挂载,让全局可以使用
Vue.prototype.$intoGoods=intoGoods;
App.mpType = 'app'
//全局混入
Vue.mixin({
data(){
return{
baseURL:'https://shop.helpetmall.com/upload/',//注册一个baseURL拼接图片地址
baseUrl:'https://shop.helpetmall.com/'
// hasLogin:''
}
}
})
const app = new Vue({
...App,
store,//挂载vuex
})
app.$mount()
边栏推荐
- WordPress建站技术笔记
- 元宇宙系列--Value creation in the metaverse
- 我在滴滴做开源
- 请问大家,MySQL全量怎么样可以提高性能呢?我这里瓶颈是在Source上,在不增加并行度的情况下,
- 2021年数据泄露成本报告解读
- posgresql 到 es 报这个错误 ,啥意思
- [QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
- STM32的HAL和LL库区别和性能对比
- Some optional strategies and usage scenarios for PWA application Service Worker caching
- 《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
猜你喜欢
随机推荐
I am doing open source in Didi
When mobile applications go overseas, is your "network optimization" holding back?
Ruoyi Ruoyi framework @DataScope annotation use and some problems encountered
使用VS Code搭建ESP-IDF环境
CopyOnWriteArrayList details
小熊派——无线联网开发
为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案
How much do you know about the intelligent operation and maintenance service of data warehouse based on DMS?
生态剧变,电子签名SaaS模式迎来新突破,网络效应加速到来
window.open不显示favicon.icon
破解数字化转型困局,企业分析协同场景案例解析
建造者模式/生成器模式
黄致绮 荣获第六季完美童模全球总决赛 全国总冠军
MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
ReentrantReadWriteLock详解
Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology
leetcode:189. 轮转数组
leetcode:187. 重复的DNA序列
QT QT 】 【 to have developed a good program for packaging into a dynamic library
从零开始搭建MySQL主从复制架构









