proxy代理的使用

65 阅读1分钟

请求

main.js

import * as request from '@/utils/http.js'


// 希望vue的组件通过this就可以访问到  $post/$get相关的方法
for(let key in request){
	Vue.prototype[key] = request[key]
}

utils http.js

let baseUrl = 'https://8bk2zhfq.lc-cn-n1-shared.com' // 域名

export const $http = function (url,method='get',data={}){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:baseUrl+url ,
			method,
			header:{
				"X-LC-Id": "8bk2ZhFQdDPgo61Tq6SQe2mP-gzGzoHsz",
				"X-LC-Key": "5T0Jx8G3fwNjgkTe7yz1PXQF",
				"Content-Type": "application/json",
			},
			data,
			success:res=>{
				resolve(res.data)
			},
			fail:err=>{
				reject(err)
			}
		})
	})
}
export const $get = function(url,data={}){
	return $http(url,'get',data)
}
export const $post = function(url,data={}){
	return $http(url,'post',data)
}
export const $delete = function(url,data={}){
	return $http(url,'delete',data)
}
export const $put = function(url,data={}){
	return $http(url,'put',data)
}

跨域--proxy---vite.config.js

server: {
    proxy: {
      '/api': {
        target: 'http://47.96.0.211:9000', //替换的服务器地址 
        rewrite: path=>path.replace(/^\/api/,'')
      },
    }
  }
 http://www.cnblogs.com/chaoyuehedy/p/9931146.html