项目背景
实现一款交互屏桌面应用软件,类似医院那张种给用户操作办理业务的应用程序。应用程序需要多点放置,根据放置地点的不同给应用做配置
开发框架:electron-vue
vue版本:v2.6.14
electron版本:v17.2.0
node版本:v16.13.0
实现
在架构的根目录下创建一个config.conf文件,作为动态配置的入口。文件内容就是正常的JSON个是就可以。例如:
{
"STADIUM_ID":"112"
}
在主进程中,定义读取配置文件的程序:
import { app } from 'electron'
const path = require("path");
const fs = require("fs");
export function getExePath() {
return path.dirname(app.getPath("exe"));
}
export function getConfigPath() {
return getExePath() + "\\config.conf";
}
export function readConfig(callback) {
fs.readFile(getConfigPath(),"utf-8",(err,data) => {
if(data) {
const config = JSON.parse(data);
callback(config)
}else {
callback({STADIUM_ID:102})
}
})
}
在ipc中设置handle事件,作为被调用的入口:
ipcMain.handle("get-params", (event, args) => {
readConfig(res => {
BrowserWindow.fromWebContents(event.sender).webContents.send(
"get-params-reply",
res
);
});
});
在渲染进程中,触发并使用参数:
ipcRenderer.invoke('get-params')
ipcRenderer.once('get-params-reply',(event,args) => {
this.getStadiumDetail(args.STADIUM_ID);
})