封装http请求
This commit is contained in:
		
							parent
							
								
									dbdcfd5128
								
							
						
					
					
						commit
						47164b6f06
					
				
							
								
								
									
										20
									
								
								src/common/http.interceptor.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/common/http.interceptor.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					// install 名字不要改 规范 => 自己做过 类似vue插件的封装 那么就会清楚为什么写install 名称
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const install = (Vue, vm) => {
 | 
				
			||||||
 | 
					  // 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Vue.prototype.$u.http.setConfig({
 | 
				
			||||||
 | 
					    // 统一的公共接口地址
 | 
				
			||||||
 | 
					    baseUrl: 'https://api-hmugo-web.itheima.net/api/public/v1',
 | 
				
			||||||
 | 
					    // 发送请求时  显示的文字 
 | 
				
			||||||
 | 
					    loadingText: '努力加载中~',
 | 
				
			||||||
 | 
					    // 如果发送请求后800内 数据还没有回来 才显示加载中
 | 
				
			||||||
 | 
					    loadingTime: 800,
 | 
				
			||||||
 | 
					    // 显示加载中
 | 
				
			||||||
 | 
					    showLoading: true,
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default {
 | 
				
			||||||
 | 
					  install
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,6 +1,10 @@
 | 
				
			|||||||
import Vue from 'vue'
 | 
					import Vue from 'vue'
 | 
				
			||||||
import App from './App'
 | 
					import App from './App'
 | 
				
			||||||
import uView from "uview-ui";
 | 
					import uView from "uview-ui";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// http拦截器,此为需要加入的内容,如果不是写在common目录,请自行修改引入路径
 | 
				
			||||||
 | 
					import httpInterceptor from '@/common/http.interceptor.js'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Vue.use(uView);
 | 
					Vue.use(uView);
 | 
				
			||||||
Vue.config.productionTip = false
 | 
					Vue.config.productionTip = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -9,4 +13,8 @@ App.mpType = 'app'
 | 
				
			|||||||
const app = new Vue({
 | 
					const app = new Vue({
 | 
				
			||||||
  ...App
 | 
					  ...App
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 这里需要写在最后,是为了等Vue创建对象完成,引入"app"对象(也即页面的"this"实例)
 | 
				
			||||||
 | 
					Vue.use(httpInterceptor, app)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app.$mount()
 | 
					app.$mount()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user