nuxt.js配置跨域
时间: 2023-09-27 22:09:23 浏览: 282
要在Nuxt.js中配置跨域,你需要在nuxt.config.js文件中添加proxy配置。在modules模块中添加'@nuxtjs/proxy',并在axios配置中设置proxy为true。然后在proxy对象中添加一个匹配路径的规则,设置changeOrigin为true,target为允许跨域的服务器地址,并使用pathRewrite来重写路径。具体配置可以参考以下示例代码:
```
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
axios: {
proxy: true
},
proxy: {
'/api': {
changeOrigin: true,
target: 'http://127.0.0.1:8082',
pathRewrite: {
'^/api': ''
}
}
}
```
这样配置后,所有以/api开头的请求都会被转发到http://127.0.0.1:8082这个地址上,实现跨域访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Nuxt.js配置proxy跨域代理以及asyncData使用方法](https://blog.csdn.net/zyxczyf123/article/details/112392436)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Nuxt.js 介绍及其使用 ·(一)基础篇,涵盖跨域介绍](https://blog.csdn.net/Android_boom/article/details/127088039)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文