uniapp的Vue2,Vue3配置跨域(proxy代理)

vue2 

找到manifest.json文件,通过源码视图的方式打开文件:在文件中添加一下代码即可完成代理:

"h5": {
	"devServer": {
		"disableHostCheck": true, //禁止访问本地host文件
		"port": 8000, //修改项目端口
		"proxy": {
			/**配置服务器路径**/
			"/api": {
				"target": "https://api.xxx.com",// 目标服务器
				"changeOrigin": true,
				/**重写路径**/
				"pathRewrite": {
					"^/api": ""
				}
			}
		}
	}
}

vue3

创建uniapp项目,在项目根目录下面创建一个名为vite.config.js的文件(如果不存在),在文件中编辑一下内容即可:

import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
 
export default defineConfig({
	plugins: [
		uni()
	],
	server: {
		port: 3000,
		proxy: {
			'/api': {
				target: 'https://api.xxx.com', // 目标服务  
				changeOrigin: true,
				rewrite: path => path.replace(/^\/api/, ''),
			}
		}
	}
})

猜你喜欢

转载自blog.csdn.net/m0_57033755/article/details/134475818