javascript - Electron 在开发中运行时显示空白屏幕,但在生产中运行

标签 javascript node.js reactjs electron create-react-app

当我在 CLI 中运行“yarn run electro-dev”时,打开 http://localhost:3000浏览器中显示主页,但 Electron 应用程序显示空白。

与其他人在生产中遇到问题但在开发中工作的类似问题不同,我遇到了相反的问题。

查看开发工具,浏览器控制台中没有错误,即使我执行 console.log() ,控制台中也没有任何内容。

Electron .js

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const edge = require('electron-edge-js');
const path = require('path');
const isDev = require('electron-is-dev');
require('electron-reload')

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
      width: 900, 
      height: 680,
      webPreferences: {
        nodeIntegration: true,
      },
  });
  console.log('isDev', isDev);
  mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
  if (isDev) {
    // Open the DevTools.
    //BrowserWindow.addDevToolsExtension('<location to your react chrome extension>');
    mainWindow.webContents.openDevTools();
  }

  mainWindow.on('closed', () => mainWindow = null);
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow();
    console.log('entering func123');
    func123();
  }
});

Package.json

{
  "name": "myelectronapp",
  "description": "Electron + Create React App + Electron Builder",
  "version": "0.1.0",
  "private": true,
  "author": {
    "name": "Andrew C",
    "email": "your.email@domain.com",
    "url": "https://your-website.com"
  },
  "build": {
    "appId": "com.andrewchoi.my-electron-app",
    "productName": "MyElectronApp",
    "copyright": "Copyright © 2019 ${author}",
    "mac": {
      "category": "public.app-category.utilities"
    },
    "files": [
      "build/**/*",
      "node_modules/**/*"
    ],
    "directories": {
      "buildResources": "assets"
    }
  },
  "dependencies": {
    "@material-ui/core": "^4.9.4",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "electron-edge-js": "^12.8.1",
    "electron-is-dev": "^1.1.0",
    "mssql-connection-string": "^0.3.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.4.0"
  },
  "main": "public/electron.js",
  "homepage": "./",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "rescripts test",
    "eject": "react-scripts eject",
    "electron-dev": "concurrently \"cross-env BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
    "postinstall": "electron-builder",
    "preelectron-pack": "yarn build",
    "electron-pack": "electron-builder build -w"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@rescripts/cli": "^0.0.13",
    "@rescripts/rescript-env": "^0.0.11",
    "concurrently": "^5.1.0",
    "cross-env": "^7.0.0",
    "electron": "8.0.1",
    "electron-builder": "22.3.5",
    "wait-on": "^4.0.0"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

最佳答案

这对我来说看起来很奇怪:

mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);

是否有理由为客户端内容使用单独的服务器,而不是像在产品中一样将索引文件作为文件资源传递?

检查另一台服务器是否正在运行。我知道当我尝试加载 Web url 但服务器未运行时会出现“空白页”效果。

关于javascript - Electron 在开发中运行时显示空白屏幕,但在生产中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60388137/

相关文章:

node.js - Mongoose 将 strict 设置为 false 会禁用验证

reactjs - 检测子引用何时发生变化

reactjs - 在 VS Code 中调试错误 : Cannot connect to the target at localhost:3000: could not find any debuggable target

reactjs - 在 React 中使用 Jest 模拟 Axios - 模拟函数未被调用

javascript - 同时使用时避免 keyup、keydown 上的多个事件

javascript - jQuery/Javascript 随机数动画

javascript - 在下拉列表中选择时显示无中断 chart.js

javascript - 我如何获取类似于数据库中字段的值?

node.js - Mongoose - 具有多个相同 ID 的 find()

javascript - Electron 应用程序 : Unable to load preload script