安装
npm i @nuxtjs/axios
安装完成后在nuxt.config中进行配置
export default{
modules: [
// https://go.nuxtjs.dev/axios
'@nuxtjs/axios'
],
// 代理设置
proxy: {
// 配置/api代理到http://localhost:3002
'/api': {
target: 'http://localhost:3002', // 目标接口域名
pathRewrite: { // 重写路径
'^/api/': '/'
},
changeOrigin: true // 是否跨域
}
},
}
此时请求的话以 /api/ 开头的请求会自动重写为 / 然后此时会代理到 http://localhost:3002 接口上。