electron-vite 抽离config.js

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目录下)

 

posted @ 2025-06-30 16:25  风花一世月  阅读(7)  评论(0)    收藏  举报