关于javascript:vue请求本地json文件configjson配置动态地址维护无需修改代码

2次阅读

共计 575 个字符,预计需要花费 2 分钟才能阅读完成。

本地应用 config.json

只须要把 config.json 文件放在 static 或 webpack3 生成的脚手架 public 里
应用 config.json 文件时,dev 或 serve 开发环境能够运行和应用,生产环境会报跨域谬误。

{"BASE_URL":"www.baidu.com"}

应用(main.js 里):

import "../public/config.json"

//webpack3/4
import "../static/config.json"

生产环境

因为无奈应用 json 了,换一种思路

在方才 config.json 的中央新建 config.js

const config = {path: "www.baidu.com", // 接口地址};
console.log(config);
sessionStorage.setItem('response', JSON.stringify(config));
console.log(JSON.parse(sessionStorage.getItem("response")).path)

应用(主入口文件 index.html)

<script src="./config.js"></script>

在须要的中央

const path = JSON.parse(sessionStorage.getItem("response")).path
正文完
 0