Electron.js 加载文件未加载

问题描述 投票:0回答:1

我有一个电子应用程序,当窗口准备好时,它会加载登录页面。然后它通过

ipcRenderer.send
接收凭据,然后我运行以下代码:

ipcMain.on('login', async (event, u, p) => {
    let result = await login(u, p)

    switch (result) {
      case 200:
        console.log('I am before loadfile') // test message to see if code runs
        mainWin.loadFile('./index.html')
        console.log('I am after loadfile') // test message to see if code runs
        break;

      default:
        mainWin.loadFile('./login.html')
        break;
    }
  })

现在...这段代码不会返回任何错误,我可以在控制台中看到指令前后的测试消息,但我不明白为什么

index.html
不加载。有趣的是......它有时会加载,但大多数时候它不会...... 主进程和渲染进程都没有错误。

似乎由于某种原因该指令被忽略了。只是它并不总是被忽略。

如有任何帮助,我们将不胜感激!

谢谢!

electron
1个回答
0
投票

按照 @Welcor 的建议(阅读下面的评论),使用

__dirname
创建绝对路径解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.