0

我在电子反应应用程序中有一个文本输入字段。windows.alert() 被用于通过在给定条件下更改状态的警报。但是在发出警报后,一个完全独立的表单中的输入文本字段将不允许用户在不点击应用程序然后再返回的情况下输入数据。

我目前确实有一个解决方法,即使用电子的 ipcRenderer 和 ipcMain 发出警报,但样式与应用程序的其余部分不匹配。有没有办法处理不会阻止数据输入到文本输入字段的 windows.alert()?是什么导致 windows.alert() 阻止将数据输入到文本输入中?

之前的代码示例:

function doSomething(value) {
    let array = otherArray;
    if (array.length == 0) {
        //The below was blocking typing into <input> in a completely separate form.
        window.alert("Please add expected value first")
    }
}

使用 preload.js 和 ipcMain 中的 ipcRenderer 解决代码:

function doSomething(value) {
    let array = otherArray;
    if (array.length == 0) {
        window.api.send("send-alert", "Please add expected value first")
    }
}

//in main.js

ipcMain.on("send-alert", (event, incomingMessage)=> {
    let options = {
         message: incomingMessage
    }
    dialog.showMessageBox(mainBrowserWindow, options)
}
4

1 回答 1

0

使用 toastr 来绕过这个。

阅读:https ://github.com/CodeSeven/toastr

触发错误或警告消息来代替 window.alert() 方法。即: toastr.warning()toastr.error()

于 2021-06-16T14:41:13.993 回答