項(xiàng)目開發(fā)中如何跨域
Vue 項(xiàng)目的代理設(shè)置
vue.config.js
module.exports = {
devServer: {
proxy: {
"/api": {
//這里最好有一個(gè) /
target: "https://rob.megameta.cn", // 后臺接口域名
secure: false, // 如果是https接口西傀,需要配置這個(gè)參數(shù)
changeOrigin: true, //是否跨域
pathRewrite: {
"^/api": ""
}
}
}
}
};
React 項(xiàng)目中的代理設(shè)置
src/setupProxy.js
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
createProxyMiddleware("/api", {
target: "https://wechat.megameta.cn/archives/",
changeOrigin: true,
pathRewrite: {
"^/api": "",
},
}),
createProxyMiddleware("/agent", {
target: "https://wechat.megameta.cn/consumer/",
changeOrigin: true,
pathRewrite: {
"^/agent": "",
},
})
);
};