内置时Electron显示白屏

Fil*_*don 1 javascript json electron electron-builder electron-packager

我正在学习电子,并且制作了一个电子应用程序,用于读取和创建文件。当我使用“ npm start”或“ electron”启动应用程序时。它按预期工作 你可以在这里看到应用程序

但是当我使用“ npm run build”或“ build -w”命令时,所构建的应用程序仅显示白屏 我的代码有问题还是我使用的命令有问题?

这是我的package.json

 {
  "name": "prova",
  "version": "1.1.3",
  "description": "Prova electron",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "dist" : "build"
  },
  "author": "Randy",
  "license": "ISC",
  "devDependencies": {
    "electron": "^2.0.2",
    "electron-packager": "^12.1.0"
  },
  "build": {
    "appId": "prova",
    "win":{
      "target" : "nsis",
      "icon" : "icon.ico"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要js页面:

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

function boot(){
    win = new BrowserWindow()
    win.loadURL(url.format({
        pathname: 'index.html',
        slashes: true
    }))
}

app.on('ready', boot);
Run Code Online (Sandbox Code Playgroud)

这是我的功能js页面:

var app = require("electron").remote;
var dialog = app.dialog;
var fs = require("fs");
var i = 0;
var stringaLetta = "";

document.getElementById("bottone").onclick = function(){
    dialog.showSaveDialog((fileName) => {
        if(fileName === undefined){
            alert("errore")
            return
        }

        var content = document.getElementById("testo").value;

        fs.writeFile(fileName, content, (err) => {
            if (err == undefined) {
                dialog.showMessageBox({
                    message: "the file has been saved",
                    buttons: ["OK"]
                });
            }
            else dialog.showMessageBox({
                message: err
            })
        })
    })
}
document.getElementById("bottone2").onclick = function(){
    dialog.showOpenDialog((fileNames) => {
        if(fileNames === undefined){
            dialog.showMessageBox({
                message: "errore durante l'apertura",
                buttons: ["OK"]
            })
            return
        } else{
            readFile(fileNames[0]);
        }
    }) 
}

function readFile(fP){
    fs.readFile(fP, 'utf-8', (err, data) => {
        if(err){
            alert(err)
            return
        }
        var textArea = document.getElementById("rtesto")
        textArea.innerHTML = "";
        i = 0;
        do{
            if(data.charAt(i) == "\n"){
                stringaLetta += "<br\>";
            }else{
                stringaLetta += data.charAt(i);
            }
            i++;
        }while(data.charAt(i) != "")
        textArea.innerHTML = stringaLetta;
        stringaLetta = " ";
    })
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*aji 6

我最近遇到白屏问题,在我的情况下略有不同

我使用了带有路由器的vue框架(路由器必须是散列的)

1.对于vue.js,在电子中使用vue路由器

const router = new VueRouter({
  mode: 'hash',
  routes: [...]
})
Run Code Online (Sandbox Code Playgroud)

2.对于react.js,在电子中使用react router

hashrouter
Run Code Online (Sandbox Code Playgroud)

代替

BrowserRouter
Run Code Online (Sandbox Code Playgroud)

没有任何框架

检查入口点 url 放置是否正确

 win.loadURL('app://./index.html')
 
Run Code Online (Sandbox Code Playgroud)


Jul*_* Z. 5

当我尝试为Windows构建时,我遇到了类似的问题。

尽管win.loadURL(...)似乎在开发中可以正常工作,但也许在构建时尝试将其更改为:

win.loadURL(url.format({
  pathname: path.join(__dirname, 'index.html'),
  protocol: 'file:',
  slashes: true
}));
Run Code Online (Sandbox Code Playgroud)

这确保它definitly到达你的正确的道路index.html文件。

为了path.join(...)工作,您require首先需要这样做:

const path = require('path');
Run Code Online (Sandbox Code Playgroud)

  • 作品!你必须包含 ```const url = require('url'); ``还有 (2认同)