uni-app 网络请求封装,统一根路径

 

创建自己的 api.js

const BASE_URL = 'https://bi.baidu.com:1234/'


export const sendRequest = (options) => {
	uni.request({
		url: BASE_URL + options.url,
		method: options.method || 'GET',
		data: options.data || {},
		success: (res) => {
			options.success(res)

		},
		fail: (err) => {
			uni.showToast({
				title: "请求接口失败"
			})
			reject(err)
		}
	})

}

main.js

import {sendRequest} from './util/api.js'
//全局挂载
Vue.prototype.$sendRequest = sendRequest

页面上调用

 

getBusStatus() {

	this.$sendRequest({
		url: "/bus/" + this.city.code + "/status",
		data: {
			'lineId': this.line.gprsId, 
			"direction": this.line.direction
		},
		success: (res) => {
			this.realBusStatus = res.data
		}
	})
}

 

你可能感兴趣的:(uni-app 网络请求封装,统一根路径)