electron 在Windows中获得黑屏后,电子建设者React电子应用程序

2wnc66cl  于 2022-12-16  发布在  Electron
关注(0)|答案(4)|浏览(226)

电子生成器React电子应用程序后窗口黑屏。
这里是package.json.在windows中获得黑屏后,电子建设者React电子应用程序.

{
  "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"
  ]
}

public/electron.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())
}

不确定为什么在生成后显示空白页。尝试了不同的方法,但没有成功
enter image description here

p1iqtdky

p1iqtdky1#

不确定您是否在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>
  )
zwghvu4y

zwghvu4y2#

以防有人像我一样偶然发现这个问题,我通过改变来解决我的问题

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
  }));

确保你需要路径和url并且也让mainwindow

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();
kh212irz

kh212irz3#

此问题是由于应用程序的构建文件夹和入口点之间的链接错误所致
请勿以这种方式使用
主窗口.loadURL(是开发人员吗?“http://localhost:3000”:(x 1m 0n 1x);
以这种方式使用
主窗口.loadURL(是开发人员吗?“http://localhost:3000”:x 1m 1n 1x);
如果您使用React路由器,则必须替换(Router)〉〉(HashRouter)

6psbrbz9

6psbrbz94#

const { app, BrowserWindow } = require('electron')

       const appURL = app.isPackaged
        ? url.format({
            pathname: path.join(__dirname, "index.html"),
            protocol: "file:",
            slashes: true,
        })
        : "http://localhost:3000";
    mainWindow.loadURL(appURL);

    if (!app.isPackaged) {
        mainWindow.webContents.openDevTools();
    }

相关问题