Electron 中自定义 HTML 对话框的问题

所以首先,我知道以下问题Custom HTML Dialog in Electron already exists。当最新版本的电子出现一些问题时,我的问题扩展了这个问题。


所以一些背景:

我实际上开始了我的一个项目,就像v2.0.5我已经拥有的 Electron() 上的一个非常旧的版本,因为我懒得更新 electron。我有一个工作对话类,你可以这样做:


let dialog = new dialog_class("./pages/dialog.html")

dialog.display().then((response) => console.log(response));

但是我必须将我的版本更新到 current( v9.1.1) 所以我的对话当然坏了,但我不知道如何/为什么。


我非常像这样创建我的对话框:


constructor(link){

    this.link = link;

    this.window = new electron.remote.BrowserWindow({...});

}

display(){

    return new Promise((callback)=>{

        this.window.loadURL(...);//url.format function in place of ...

        this.window.on(`close`, () => {

            if (!this.cancelCloseEvent) callback(false);

        });

    }

}

destroy(){

    this.window.closable = true;

    this.widow.close();

}

但是,当我运行此功能时:


function openDialog(){

    let dialog = new dialog_class("./pages/dialog.html")

    dialog.display().then((response) => console.log(response));

}

我可以完美打开对话框,但只能关闭一次对话框。就像我可以打开它,关闭它然后再次打开它但不能再次关闭它。当我第二次尝试关闭它时,它会保持对话框打开并抛出这个:


electron/js2c/renderer_init.js:82 Uncaught TypeError: Object has been destroyed

    at BrowserWindow.get (electron/js2c/browser_init.js:125)

    at electron/js2c/browser_init.js:233

    at IpcMainImpl.<anonymous> (electron/js2c/browser_init.js:233)

    at IpcMainImpl.emit (events.js:223)

    at WebContents.<anonymous> (electron/js2c/browser_init.js:173)

    at WebContents.emit (events.js:223)

我不知道为什么会这样,因为每次我运行openDialog它都应该创建一个新的 BrowserWindow,所以我不知道它是如何引用旧窗口的。


注意:这里显示的所有代码应该足以解决我的问题。但以防万一这里是整个dialog_class:https ://pastebin.com/7pAwZJHF


元芳怎么了
浏览 407回答 1
1回答

HUH函数

dialog.jsdisplay(context) {&nbsp; &nbsp; &nbsp; &nbsp; return new Promise((cb) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; electron.ipcRenderer.on("callback", (event, val) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._cancelCloseEvent = true;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb(val);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this.destroy(); WRONG!!! Remove this and destory your window on test.js which is having this object instance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ...test.jsdialog.display().then(function(value) {&nbsp; &nbsp; response.innerText = value;&nbsp; &nbsp; dialog.destroy();&nbsp; &nbsp; // dialog = null; unnecessary!});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript