当前位置:首页 > 未分类

UniApp H5 跨域代理配置并使用

admin2年前 (2024-08-26)未分类555

UniApp H5 跨域代理配置并使用


一、方式一:修改 manifest.json 文件

在 UniApp 找到 manifest.json -》源码视图,添加 h5 配置项:

"h5" : {
    "devServer" : {
        "disableHostCheck" : true,
        "proxy" : {
            "/api" : {
                "target" : "http://www.dzm.com",
                "changeOrigin" : true,
                "secure" : false,
                "ws": false,
                "pathRewrite" : {
                    "^/api" : ""
                }
            }
        }
    }}


二、方式二:添加 vue.config.js 文件

像 vue 开发一样,手动创建一个 vue.config.js 文件,然后添加上代理,vue.config.js 只能创建在项目的根目录,不然会无法识别到。

注意:UniApp 会识别 vue.config.js 文件,但是 manifest.json 的优先级
要高于 vue.config.js 文件,所以看需求选择一个配置即可。

module.exports = {
    devServer: {
        disableHostCheck: true,
        proxy: {
            '/api': {
                target: 'http://www.dzm.com',
                changeOrigin: true,
                secure: false,
                ws: false,
                pathRewrite: {
                    '^/api': ''
                }
            }
        }
    }}


扫描二维码推送至手机访问。

版权声明:本文由前端开发技术分享发布,如需转载请注明出处。

本文链接:https://www.jqkcms.fun/?id=7

分享给朋友:

相关文章

vue 子组件调用父组件方法

示例:    父组件:   ...(略)   <子组件 @触发的方法="父组件方法"></子组件>   ...(略)  &nbs…

VUE开发报错:Avoid mutating a prop directly since the value will be overwritten whenever the parent ...

VUE开发报错:Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed p…

微信小程序 -- 由于隐私协议未设置,影响隐私相关api调用

在app.json中,usePrivacyCheck设置为false,可以免除在开发者工具中调试时的影响,开发版暂未测试。…

VUE开发报错:Module build failed (from ./node_modules/ ...

VUE开发报错:Module build failed (from ./node_modules/@dcloudio/vue-cli-plugin-uni/packages/vue-loader/lib/loaders/templateLoader.js):  语法错误: Une…

vue项目中配置不同的环境是否显示vConsole

前言为了让测试人员能够在手机、平板上也能看到像浏览器f12那样的调试工具,便于查看日志数据和网络请求等,专门搜了一下,发现这个vConsole官方的调试工具真的很强大,十分好用。vConsole的简介…

Component template should contain exactly one root element. If you are using v-if on multiple eleme

情境描述今天使用vue框架做项目的时候,突然出错Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to c…