【问题标题】:Electron windows is blank on load电子窗口在加载时为空白
【发布时间】:2020-09-17 22:58:33
【问题描述】:

我想在电子应用程序中嵌入一些 php 服务器端代码。我找到了一种让 php 在我的应用程序中工作的有趣方法,但是当应用程序启动时我遇到了一个小问题。事实上,在我刷新它之前,主窗口是空白的。是否可以刷新页面来解决这个问题?

const { app, BrowserWindow } = require('electron');
const electron = require('electron');
const Menu = electron.Menu;
const path = require('path');
const url = require('url');
const os = require('os');
// PHP SERVER CREATION /////
const PHPServer = require('php-server-manager');
var php = 'php';
// if(os.platform === 'win32' && os.arch === 'ia32'){
//    php = path.resolve(__dirname)+'/php/x86/php.exe';
// }
// if(os.platform === 'win32' && os.arch === 'x64'){
//    php = path.resolve(__dirname)+'/php/x64/php.exe';
// }
const server = new PHPServer({
  php: php,
  directory: path.resolve(__dirname)+'/',
  directives: {
      display_errors: 1,
      expose_php: 0
  }
});
app.on('ready', () => {
  createWindow();
  server.run();
  mainWindow.reload();
  // if (os.platform === 'darwin') {
  // }
});

let mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow({width: 1024, height: 620, });
  mainWindow.center();
  mainWindow.loadURL('http://127.0.0.1:8000/index.php');
  mainWindow.webContents.openDevTools();
  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // PHP SERVER QUIT
      server.close();
      mainWindow = null;
    });
  }
  app.on('window-all-closed', function () {
    if (os.platform !== 'darwin') {
      // PHP SERVER QUIT
      server.close();
      app.quit();
    }
  });
  app.on('activate', function () {
    if (mainWindow === null) {
      createWindow();
    }
  });

更新:

经过一番研究,我找到了一种加载窗口内容的方法,空白窗口的问题与php服务器管理器模块的script参数有关。在我将它指向作为我的应用程序路由器的 index.php 之后,内容已加载,但现在我有两个控制台错误:

jquery.min.js:1 Uncaught SyntaxError: Unexpected token <1>

    标签: php electron


    【解决方案1】:

    我建议尝试从ready-to-show 事件开始挂钩您的服务器。

    mainWindow.on('ready-to-show', () => {
       server.run();
       mainWindow.show()
    })
    

    【讨论】:

    • 我会尝试你的建议。
    • 它不工作。如果我将服务器设置为在主窗口准备好显示时运行,它将不会运行。
    • @dwpu - 早点开始?
    • @dwpu - 不,我只是建议一些容易尝试的东西。启动并运行服务器,然后打开显示窗口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多