1、將config.js 放到resources下的config目錄下
module.exports = {url: 'http://192.168.1.17:8000',wsUrl: 'ws://192.168.1.17:8000',
}
2、在preload.js 暴露讀取API? src/preload/index.js(或你的preload入口)
const fs = require('fs');
const path = require('path');function getConfigPath() {// __dirname 是 win-unpacked/resources/app// return path.join(__dirname, 'config.js');//你的config地址let configPath = path.resolve('./resources/app.asar.unpacked/resources/config/config.js');//單獨配置測試環境if (process.env.NODE_ENV === 'development') {configPath = path.resolve('./resources/config/config.js');}return configPath;
}
3、在代碼中,或者vue文件中調用
// 任何地方都可以
const config = window.electron.readConfig();
console.log(config.url);
注:修改config.js文件需要重新獲取啟動exe,只有免安裝版本可以修改(win-unpacked目錄下)