【问题标题】:How can i make electron run code in the right order?如何以正确的顺序制作电子运行代码?
【发布时间】:2020-02-13 20:50:35
【问题描述】:

当我在后端电子代码中调用此函数logout(true) 时,对话框会在渲染器完成加载新页面之前显示,无论如何我可以让此代码以页面加载的正确顺序运行,然后出现错误消息发送了吗?

function logout(authFail) {
  win.loadFile(path.join(__dirname, 'src','login.html'));
  currentusername = null
  currentpassword = null
  if (authFail == true) {
    dialog.showErrorBox("Error","Unauthorised Access!")
  }
}

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    win.loadFile 在大多数情况下是异步操作,因此您必须等待 login.html 被加载。

    我想你可以使用

    win.once('ready-to-show', () => {
      if (authFail == true) {
        dialog.showErrorBox("Error","Unauthorised Access!")
      }
    })
    

    https://www.electronjs.org/docs/api/browser-window#using-ready-to-show-event

    【讨论】:

    • 非常感谢,我没有考虑过使用 ready 事件,但这对我来说不太适用,因为似乎只在第一次加载渲染器时才发送一次准备显示并且没有再次发送,但是在您让我走上正确的轨道后我能够得到一个可行的解决方案,现在将添加它
    【解决方案2】:

    非常感谢 Andrea Franchini 的回答让我走上了正确的轨道,但似乎只发送了一次准备展示,但是经过一些研究后,我发现这个解决方案对我有用:

    function logout(authFail) {
      win.loadFile(path.join(__dirname, 'src','login.html'));
      currentusername = null
      currentpassword = null
      win.webContents.on('did-finish-load', () =>{
        if (authFail == true) {
          dialog.showErrorBox("Error","Unauthorised Access!")
        }
      })
    }
    

    参考: https://stackoverflow.com/a/50980097/12887221

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 2017-01-19
      相关资源
      最近更新 更多