当前位置:网站首页>使用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()
边栏推荐
猜你喜欢
一文看懂推荐系统:召回03:基于用户的协同过滤(UserCF),要计算用户之间的相似度
Difference and performance comparison between HAL and LL library of STM32
【Unity入门计划】基本概念(7)-Input Manager&Input类
Windows 事件查看器记录到 MYSQL
使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)
I am doing open source in Didi
为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案
我在滴滴做开源
STM32的HAL和LL库区别和性能对比
Introduction to spark learning - 1
随机推荐
leetcode:187. 重复的DNA序列
CPU个数_核心数_线程数之间的关系
smp,numa和mpp体系结构总结
unity用代码生成LightProbeGroup
托尔斯泰:生活中只有两种不幸
protobuf 中数据编码规则
高可用版 主数据库数据结构改变 备数据库会自动改变吗
【Unity入门计划】基本概念(7)-Input Manager&Input类
Leetcode76. 最小覆盖子串
1、实例开启无锁表结构变更以后,在任务编排中通过“单实例SQL”节点进行的结构变更,是优先采用无锁表
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)
Ruoyi Ruoyi framework @DataScope annotation use and some problems encountered
WordPress建站技术笔记
leetcode-693.交替位二进制数
mysql delete 执行报错:You can‘t specify target table ‘doctor_info‘ for update in FROM clause
CopyOnWriteArrayList详解
参与便有奖,《新程序员》杂志福利来袭!
为什么我强烈推荐使用智能化async?
DataGrip:非常好用的数据库工具,安装与使用教程,亮点介绍