当前位置: 首页 > Web前端 > HTML5

vue请求本地json文件,config.json配置动态地址,维护不需要修改代码

时间:2023-04-05 17:51:27 HTML5

本地使用config.json只需要将config.json文件放在静态或者webpack3生成的公共脚手架中即可使用config.json文件,dev或者serve开发环境可以运行使用,但是生产环境会报跨域错误。{"BASE_URL":"www.baidu.com"}使用(在main.js中):import"../public/config.json"//webpack3/4import"../static/config.json"因为生产环境无法使用json,另一种思路是新建config.jsconstconfig={path:"www.baidu.com",//接口地址};console.log(config);sessionStorage.setItem('response',JSON.stringify(配置));console.log(JSON.parse(sessionStorage.getItem("response")).path)在需要的地方使用(主入口文件index.html)constpath=JSON.parse(sessionStorage.getItem("response")).路径