Closed kaizige10 closed 3 years ago
我的vite配置类似这样:
server: { host: serverJson.domain, port: serverJson.httpsPort, strictPort: true, proxy: { '^/ShoppingMall/api/': { target: 'http://1.2.3.4:8080/', changeOrigin: true, ws: true, secure: false, rewrite: (path) => { let _path = path; _path = path.replace(/\/ShoppingMall/, "/"); console.log(`[vite.config.js proxy] rewrite path : ${_path}`); return _path; }, }; }, plugins: [ // 支持本地mock viteMockServe({ mockPath: 'mock', localEnabled: true, watchFiles: true, logger: true, }), ], }
mock文件夹下有一个test.js:
export default [ { url: '/api/queryProductList', method: 'post', response: ({ query }) => { return { retCode: 0, total: 1, productInfoList: [ { id: '1', productType: 'phone', productName: 'xiaomi 11', }, ], }; }, }, ];
原本这样做,请求都会转发到后台服务的http://1.2.3.4:8080/api/xxxx,我加了vite-plugin-mock以后,没有什么变化,还是请求到后台http://1.2.3.4:8080/api/xxxx
应该是我的用法不对,我再看下源码,看看如何解决
确实是用法不对,我的mock文件里面,url是写死的'/api/queryProductList',实际上我收到的请求是/ShoppingMall/api/queryProductList,所以把url换成正则去匹配就可以了:url: /.*api\/queryProductList$/
我的vite配置类似这样:
mock文件夹下有一个test.js:
原本这样做,请求都会转发到后台服务的http://1.2.3.4:8080/api/xxxx,我加了vite-plugin-mock以后,没有什么变化,还是请求到后台http://1.2.3.4:8080/api/xxxx