根目录public下新建config.json配置文件,如:
{"VITE_URL_3DTILES_BIG":"http://192.168.1.1:88/abu1/tW4fYCyXi/tileset.json","VITE_URL_3DTILES_SMALL":"http://192.168.1.1:181/3dtile_200/tileset.json","VITE_URL_IMAGERY_BING":"http://192.168.1.1/bing/{z}/{x}/{y}.jpg","VITE_URL_IMAGERY_ARCGIS":"http://192.168.1.1:181/arcgis/{z}/{x}/{y}.jpg","VITE_URL_IMAGERY_GOOGLE":"http://192.168.1.1:181/google/{z}/{x}/{y}.jpg"
}
在模板中用请求的方式获取他:
let config=ref();
async function loadConfig() {const response = await axios.get('/config.json');config.value = response.data;
};
loadConfig();
为了确保正确赋值,需要watch一下config的值再赋值
let tile=ref();
watchEffect(() => {if (config.value && Object.keys(config.value).length > 0 ) {tile.value=[{key:'someTileKey',url:config.value.VITE_URL_3DTILES_BIG},{key:'small',url:config.value.VITE_URL_3DTILES_SMALL}]//执行使用到tile变量的函数fetchData();loadImagery()}
});
如果使用到tile变量的函数需要onMounted后执行的话,将获取config.json的函数放在onMounted中执行:
onMounted(() => {loadConfig();
})
打包后config.json在打包目录的根目录下,而且可修改