uni-app 调用接口封装文档
生活随笔
收集整理的這篇文章主要介紹了
uni-app 调用接口封装文档
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.創建 util 文件夾 ,并在文件夾中創建 api.js
const BASE_URL = 'http://localhost:8082' // 域名頭 export const myRequest = (options)=>{return new Promise((resolve,reject)=>{uni.request({url:BASE_URL+options.url,method: options.method || 'GET',data: options.data || {},success: (res)=>{if(res.data.status !== 0) {return uni.showToast({title: '獲取數據失敗'})}resolve(res)},fail: (err)=>{uni.showToast({title: '請求接口失敗'})reject(err)}})}) }2.在main.js 中進行全局設置
import Vue from 'vue' import { myRequest } from './util/api.js' Vue.prototype.$myRuquest = myRequest //保存在 VUE 的原型中3.接口調用結果
onLoad() {this.getSwipers() }, methods: {// 獲取輪播圖的數據async getSwipers () {const res = await this.$myRuquest({url: '/api/getlunbo'})this.swipers = res.data.message // 過去接口調用后的數據}, }總結
以上是生活随笔為你收集整理的uni-app 调用接口封装文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: element组件库中table自定义分
- 下一篇: 数据轮播图翻页封装(左右点击)