uni-app配置代理解决跨域问题
日期:2020-01-21
来源:程序思维浏览:9709次
这几天用uni-app做微信公众号和微信小程序,网页版的微信公众号需要配置代理解决跨域问题,那么如何配置呢?下面和大家分享一下:
打开HbuilderX->manifest.json,点击源码视图,如图:
填写一下代码:
"h5" : {
"template" : "index.html",
"title" : "好运买点餐",
"router" : {
"base" : "/"
},
"devServer":{
"proxy":{
"/api":{
"target":"http://localhost:9191/api",
"changeOrigin":true,
"pathRewrite":{
"^/api":""
}
}
}
}
}
devServer->proxy这里面就是配置的代理和webpack是一样的。
那么我们如何配置开发者环境和生产环境呢,需要注意的是微信小程序不支持配置代理所以只能是生产环境。
我们可以建立static/conf/config.js文件自己写一下程序让他自动识别,代码如下:
let baseApi="";
//#ifdef H5
baseApi=process.env.NODE_ENV === 'development'?"/api":"https://vueshop.glbuys.com/api";
//#endif
//#ifndef H5
baseApi="http://localhost:9191/api";
//#endif
export default {
baseApi:baseApi
}
然后导入使用就行了,学过vue和react的同学应该都知道如何使用了。
打开HbuilderX->manifest.json,点击源码视图,如图:
填写一下代码:
"h5" : {
"template" : "index.html",
"title" : "好运买点餐",
"router" : {
"base" : "/"
},
"devServer":{
"proxy":{
"/api":{
"target":"http://localhost:9191/api",
"changeOrigin":true,
"pathRewrite":{
"^/api":""
}
}
}
}
}
devServer->proxy这里面就是配置的代理和webpack是一样的。
那么我们如何配置开发者环境和生产环境呢,需要注意的是微信小程序不支持配置代理所以只能是生产环境。
我们可以建立static/conf/config.js文件自己写一下程序让他自动识别,代码如下:
let baseApi="";
//#ifdef H5
baseApi=process.env.NODE_ENV === 'development'?"/api":"https://vueshop.glbuys.com/api";
//#endif
//#ifndef H5
baseApi="http://localhost:9191/api";
//#endif
export default {
baseApi:baseApi
}
然后导入使用就行了,学过vue和react的同学应该都知道如何使用了。
精品好课