2017-07-22 88 views
0

從一個電子窗口將數據發送到另一棟樓我的第一個電子的應用程序,我想工作流如下:試圖通過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> 

回答

1

你調用順序是不正確的。你叫

win.webContents.send('input-received', data) 

index.html尚未重新裝入win

爲了解決這個問題,你應該換了電話,並確保內容已準備好當你發送IPC消息

ipcMain.on('input-broadcast', (evt, data) => { 
    console.log('input-broadcast happened in main') 
    win.loadURL(url.format({ 
     pathname: path.join(__dirname, 'index.html'), 
     protocol: 'file:', 
     slashes: true 
    })) 
    win.webContents.once('dom-ready',() => { 
     win.webContents.send('input-received', data) 
    }) 
})