安装
npm install @nuxtjs/axios @nuxtjs/proxy --save
nuxt.conf文件:
modules: [
'@nuxtjs/axios','@nuxtjs/proxy'
],
axios: {
proxy: true, // 表示开启代理
prefix: '/api', // 表示给请求url加个前缀 /api
credentials: true // 表示跨域请求时是否需要使用凭证
},
proxy: {
'/api': {
target: 'http://172.28.194.52:3000', // 目标接口域名
changeOrigin: true, // 表示是否跨域
pathRewrite: {
'^/api': '/', // 把 /api 替换成 /
}
}
}
可以直接使用
this.$axios.get('/接口地址')
.then(res => {
console.log(res)
})
.catch(e => {
console.log(e)
})
}