uview 封装http

uview封装http (2步)

  1. 创建http.interceptor.js
const baseUrl=''

const install=function(Vue,vm){
	// http基础配置
	Vue.prototype.$u.http.setConfig({
		baseUrl,
		loadingText:"正在努力加载...",
		loadingTime:800
	})
	
	// 配置请求拦截
	Vue.prototype.$u.http.interceptor.request=function(config){
		config.header.token='token'
		return config;
		// 如果返回的是false 则会取消当前的请求
		// if(config.url==='login/logout')return false;
	}
	// 配置响应拦截
	Vue.prototype.$u.http.interceptor.response=function(response){
		vm.$u.toast('响应成功');
		return response
		// 如果返回的是false , 接口处则需要通过catch(e=>{})来接收
		// if(response.code !==200) return false;
	}
}

export default {install}
  1. 将文件引入main.js
// 全局配置http
import HttpInterceptor from './common/http.interceptor.js';
Vue.use(HttpInterceptor,app)

封装api

你可能感兴趣的:(uniapp,http,vue.js,uniapp,uview)