vue3.0加上Typescript进行跨域
在项目的根目录下创建一个vue.config.js
文件,在文件中进行配置如下:
module.exports = {
devServer: {
host: 'localhost', //设置本地服务器 选填
port: 8081, //设置本地默认端口 选填
proxy: { //设置代理,必须填
'/api': { //设置拦截器 拦截器格式 斜杠+拦截器名字,名字可以自己定
target: 'http://localhost:3300', //代理的目标地址,这是豆瓣接口地址网址
changeOrigin: true, //是否设置同源,输入是的
pathRewrite: { //路径重写
'/api': '/', //路径转发代理 /api 的意思就是 用/api 代替http:localhost:8080
}
},
'/cloud': {
target: 'http://localhost:3000',
changeOrigin: true,
pathRewrite: {
'^/cloud': '',
}
}
}
}
}
然后在需要进行请求的地方进行使用/api/url
即可。
觉得有用的小伙伴可以点一下赞哦!!!🙇