代码之家  ›  专栏  ›  技术社区  ›  Ivan Rosales

ElectronJS-Vite-生产中的反应/路线问题

  •  0
  • Ivan Rosales  · 技术社区  · 1 年前

    在电子生成器反应电子应用程序后,在窗口中获得空白屏幕。

    这是package.json。在电子生成器反应电子应用程序后,窗口中出现空白屏幕。

          {
      "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/electro.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

    0 回复  |  直到 4 年前
        1
  •  8
  •   PeterP    3 年前

    不确定你是否在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>
      )
    
        2
  •  6
  •   DeeDee    4 年前

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

    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,并允许主窗口

    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();
    
        3
  •  3
  •   omarAtef    2 年前

    这个问题是由于生成文件夹&应用程序的入口点

    不要以这种方式使用

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

    以这种方式使用

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

    如果您使用React路由器,则必须更换(路由器)>>(哈希路由器)

        4
  •  0
  •   Andres Reibel    2 年前
    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();
        }