我在用vue-cli
webpack-simple
模板来生成我的项目,我想将请求代理到单独的后端服务器。如何才能轻松实现这一目标?
In @vue/cli 3.x:
- 创建一个
vue.config.js
文件在root项目的文件夹(如果您还没有)。
- 其内容如下:
// vue.config.js
module.exports = {
devServer: {
proxy: {
"/gists": {
target: "https://api.github.com",
secure: false
}
}
}
};
现在任何调用(假设您的开发服务器位于localhost:8080
) http://localhost:8080/gists
将被重定向到https://api.github.com/gists
.
另一个例子:代理所有调用
假设您有一个本地后端服务器,通常部署在localhost:5000
并且您希望将所有呼叫重定向至/api/anything
到它。使用:
// vue.config.js
module.exports = {
devServer: {
proxy: {
"/api/*": {
target: "http://localhost:5000",
secure: false
}
}
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)