從一個電子窗口將數據發送到另一棟樓我的第一個電子的應用程序,我想工作流如下:試圖通過IPC
主窗口中打開 - >用戶點擊「打開」按鈕 - >第二窗口打開 - >用戶輸入/點擊提交 - > mainWindow打開備份顯示用戶輸入
以下是我的app.on('ready'
)從我的main.js
。應用程序啓動(win.loadURL
)工作正常,open-new-window
事件也是如此。奇怪的是在input-broadcast
。
當用戶在第二個窗口中輸入輸入時,主窗口將重新打開。但是,input-broadcast
中的console.log
中的文本未出現,並且input-received
從不在主窗口的渲染器中激發。
不知道我在做什麼錯,但是我可能會使用錯誤的設計模式。任何幫助將不勝感激!
main.js
const {app, BrowserWindow, ipcMain, remote} = require('electron');
const url = require('url');
const path = require('path');
const countdown = require('./countdown');
let win;
const windows = [];
app.on('ready',() =>{
console.log('app ready');
ipcMain.on('open-new-window',() =>{
console.log('trying to open window');
win.loadURL(url.format({
pathname: path.join(__dirname, 'enterValue.html'),
protocol: 'file:',
slashes: true
}));
});
ipcMain.on('input-broadcast', (evt, data) =>{
console.log('input-broadcast happened in main');
win.webContents.send('input-received', data);
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
});
win = new BrowserWindow({width: 800, height: 600});
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
win.on('closed',() =>{
console.log('closed');
win = null;
});
})
renderer.js(與index.html的ASSOC。)
console.log('from renderer1');
const {ipcRenderer} = require('electron');
document.getElementById('start').addEventListener('click',()=>{
ipcRenderer.send('open-new-window');
console.log('window button clicked');
});
ipcRenderer.on('open-new-window', (evt, count) =>{
document.getElementById('userInput').innerHTML(count);
});
ipcRenderer.on('input-received', (evt, data) =>{
console.log('input received');
console.log(evt);
console.log(data);
});
renderer2.js(締合。與用戶enterValue.html )
console.log('from renderer2');
const {ipcRenderer} = require('electron');
document.getElementById('submitButton').addEventListener('click', (evt, input)=>{
var input = document.getElementById('randomInput').value;
ipcRenderer.send('input-broadcast', input);
});
01發送輸入時回
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>Your input was <span id="userInput"></span><p>
<button id="start">Open</button>
<script>require('./renderer')</script>
</html>
enterValue.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Enter a name</p>
<input type="text" id="randomInput" placeholder="enter a value"/>
<button id="submitButton">Submit</button>
<script>require('./renderer2.js')</script>
</body>
</html>