电子生成器对电子应用程序做出react后,在 Windows 中出现空白屏幕

IT技术 javascript reactjs electron electron-builder
2021-05-15 00:28:21

在电子生成器对电子应用程序做出react后,在 Windows 中出现空白屏幕。

这是 package.json。在电子生成器对电子应用程序做出react后,在 Windows 中出现空白屏幕。

      {
  "name": "SmallBusinessManagement",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "^4.5.3",
    "cross-env": "^7.0.2",
    "date-fns": "^2.16.1",
    "electron-is-dev": "^1.2.0",
    "jquery": "^3.5.1",
    "jspdf": "^2.1.1",
    "jspdf-autotable": "^3.5.13",
    "knex": "^0.21.12",
    "moment": "^2.29.1",
    "popper.js": "^1.16.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-popper": "^2.2.4",
    "react-redux": "^7.2.2",
    "react-router-dom": "^5.2.0",
    "redux": "^4.0.5",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "sqlite3": "^5.0.0",
    "wait-on": "^5.2.0",
    "web-vitals": "^0.2.4"
    
  },
  "scripts": {
    "postinstall": "electron-builder install-app-deps",
    "dist": "electron-builder",
    "pack": "electron-builder --dir",
    "react-start": "react-scripts start",
    "react-build": "react-scripts build",
    "react-test": "react-scripts test",
    "react-eject": "react-scripts eject",
    "electron-build": "electron-builder",
    "release": "yarn react-build && electron-builder --publish=always",
    "start": "concurrently \"cross-env BROWSER=none yarn react-start\" \"wait-on http://localhost:3000 && electron .\""
  },
  "build": {
    "productName":"SmallBussinessManagement",
    "appId": "com.smallbusinessmanagement",
    "dmg": {
      "contents": [
        {
          "x": 110,
          "y": 150
        },
        {
          "x": 240,
          "y": 150,
          "type": "link",
          "path": "/Applications"
        }
      ]
    },
    "win": {
      "target": "NSIS",
      "icon": "build/icon.ico"
    },
    "directories": {
      "buildResources": "resources",
      "output": "release"
  }
  },

  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "description": "Small business management is used to manage daily sell and buy.",
  "author": "rakshithgowda KV",
  "main": "public/electron.js",
  "homepage": "./addItem",
  "devDependencies": {
    "electron": "latest",
    "electron-builder": "latest"
  },
  "files": [
    "*.js",
    "build",
    "node_modules"
  ]
}

公共/电子.js。我需要安装任何依赖吗?

const { app, BrowserWindow,Menu } = require('electron')
require('../src/messgaeController/main');
const path = require("path");
const isDev = require("electron-is-dev");

let addItem ;
let win;
function createWindow () {
   win = new BrowserWindow({
    width: 800,
    height: 600,
    icon:"build/icon.ico",
    webPreferences: {
      nodeIntegration: true,
      // webSecurity: false
    }
  })

  win.loadURL(isDev? "http://localhost:3000": `file://${__dirname}/../build/index.html`);
    win.on("closed", () => (mainWindow = null));

  const mainMenu = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(mainMenu);
  // win.webContents.openDevTools()

  win.on("close",()=>app.quit())
}

不知道为什么它在构建后显示空白页。尝试了不同的方法,没有用

在此处输入图片说明

2个回答

如果有人像我一样偶然发现了这个,我通过改变解决了我的问题

win.loadURL(isDev? "http://localhost:3000": `file://${__dirname}/../build/index.html`);

mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
  }));

确保您需要路径和网址,并让主窗口

const path = require("path");
const url = require('url');

let mainWindow;

我也改变了

win.on("closed", () => (mainWindow = null));

mainWindow.on("closed", () => (mainWindow = null));

我还将路由器历史记录更改为哈希

import { createHashHistory } from 'history';
export default createHashHistory();

不确定你是否在 ReactJS 中实现了路由,但我通过使用 HashRouter 而不是 BrowserRouter 来修复......例如:

return (
    <HashRouter>
        <div>
        <Header/>
            <Switch>
             <Route path="/" component={Home} exact/>
             <Route path="/news" component={News} exact/>
             <Route path="/store" component={Store} exact/>
             <Route path="/login" component={Login} exact/>
             <ProtectedRoute path="/profile" component={Profile} exact/>
           </Switch>
        </div> 
      </HashRouter>
  )