1. 程式人生 > >electron WebView向子頁面出啊地數據的方法

electron WebView向子頁面出啊地數據的方法

comm 頁面 web 異步 fun file req .html getc

我用到了2種方式,

1.和瀏覽器裏一樣通過 URL或是llocalstorage 等等

2.我也是剛接觸electron 沒幾天, 就查到了一種方式 通過webContents監聽did-finish-load,然後send().

文檔上是這麽講的

Event: ‘did-finish-load‘

當導航完成時發出事件,onload 事件也完成.

然後在這個監聽裏面寫 webContents.send();

webContents.send(channel[, arg1][, arg2][, ...])

  • channel String
  • arg (可選)

通過 channel 發送異步消息給渲染進程,你也可發送任意的參數.參數應該在 JSON 內部序列化,並且此後沒有函數或原形鏈被包括了.

渲染進程可以通過使用 ipcRenderer 監聽 channel 來處理消息

 function showDetailPage(index) {
 	try {
        let win = new BrowserWindow({
                    width: 800,
                    height: 500,
                    show: false,
                    maximizable: false,
                    minimizable: false,
                    resizable: false,
                    useContentSize: true,
                    //parent: currentWindow,
                    modal: false
                });
				
        		let dataJson=JSON.stringify(dataSource.data());
                // win.webContents.openDevTools();
                win.on(‘closed‘, function () { win = null })
                win.loadURL(‘file://‘ + __dirname + ‘/AnnouncementDetail.html?index=‘+index)//指定渲染的頁面
                win.webContents.on(‘did-finish-load‘, function(){
			        win.webContents.send(‘dataJsonPort‘, dataJson);
			    });
                win.show();//打開一個窗口
  				win.webContents.openDevTools();

        // Open in Windows Client.
        //bound.openReportDetailPage(JSON.stringify(dataSource.data()), index);
      } catch(e) {

        console.log(e);
      }
    }

 這裏發送了數據 dataJson,在子頁面用

     remote,
        ipcRenderer
    } = require(‘electron‘);
    const BrowserWindow = require(‘electron‘).remote.BrowserWindow;
    const currentWindow = remote.getCurrentWindow();
        pIndex = getQueryUrlString(‘index‘);
    ipcRenderer.on(‘dataJsonPort‘, function(event, message) { // 監聽父頁面定義的端口
        initTable(message, pIndex); 把數據傳給函數 initTable
    });

  

我也剛接觸這個框架, 如果有不對的地方,還請大家多多交流

electron WebView向子頁面出啊地數據的方法