18910140161

在vue项目中使用代理进行跨域解决

顺晟科技

2021-08-29 09:38:10

190

使用Vue开发项目时,域之间总是会出现问题,因此打包时可以使用代理反向代理来解决域之间的问题。

Vue-cli2配置如下:

在Config文件夹中查找index.js

Dev: {

//Paths

AssetsSubDirectory: ' static ',

Assetspublicpath 3360 '/',

ProxyTable: {

/api': {

target : ' http://Moby . x botech.com ',//a编写实际的后台接口

ChangeOrigin: true,//允许跨域

安全: false,

pathrewrite 3360 {//写请求路径

/API' : ' '

}

}

},

请求路径

Const service=axios.create({

默认URL :' API ',//process.env.vue _ app _ base _ API,

//baseurl 3360 process . env . vue _ app _ base _ API,//API,

//with credentials : true,//send cookies when cross-domain requests

time out : 30000//request time out

})。

在Vue-cli3版本3或更高版本中,您需要创建自己的vue.config.js文件以进行配置

Module.exports={

Dev server : { host 3360 ' 0 . 0 . 0 . 0 ',//允许外部IP访问

端口: 9080,//端口

Https: false,启用//https

代理: {

/api': {

target : ' http://Moby . x botech.com/API/log in ',

ChangeOrigin: true,

安全: false,

PathRewrite: {

/API' :'/API '

}

}

}

}

}

使用Axios发送请求

Aixos.get('/api/list ')

摘要:“/api”是匹配项,目标是请求的地址。由于在Ajax的URL中添加了“/api”前缀,并且原始接口中没有此前缀,因此必须通过pathRewrite重新编写地址。如果您自己的接口地址有“/api”的公共前缀,则可以删除pathRewrite。

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航