view2
Find the manifest.json file and open the file through source view: add some code to the file to complete the proxy:
"h5": {
"devServer": {
"disableHostCheck": true, //禁止访问本地host文件
"port": 8000, //修改项目端口
"proxy": {
/**配置服务器路径**/
"/api": {
"target": "https://api.xxx.com",// 目标服务器
"changeOrigin": true,
/**重写路径**/
"pathRewrite": {
"^/api": ""
}
}
}
}
}
view 3
Create the uniapp project, create a file named vite.config.js under the project root directory (if it does not exist), and edit the content in the file:
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/, ''),
}
}
}
})