简体   繁体   English

电子窗口显示屏幕截图

[英]Electron window shows a screenshot of the screen

So I have an electron app named main.js which I start with npm start . 所以我有一个名为main.js的电子应用程序,我从npm start I've set the start script in package.json to electron main.js and have also tried electron . 我已经将package.json中的启动脚本设置为electron main.js ,还尝试了electron . . When running npm start , everything starts without any errors but the electron window only shows a snapshot of what was on the screen when I started it. 当运行npm start ,一切都开始,没有任何错误,但是电子窗口仅显示我启动时屏幕上的快照。 I've tried refreshing it but nothing seems to work. 我尝试刷新它,但似乎没有任何效果。 Here is how it looks: Image It should view localhost:3001 but it doesn't. 它的外观如下: Image它应该查看localhost:3001但不是。 I've also tried to run electron . 我也尝试过使用electron . directly in the terminal but that gives me electron: command not found . 直接在终端上,但是给了我electron: command not found When running ./node_modules/electron/dist/electron . 当运行./node_modules/electron/dist/electron . it starts as it should but the same problem occurs. 它会按预期启动,但会出现相同的问题。 Here is main.js : 这是main.js

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const core = require('./app');

let mainWindow

function createWindow() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: { webSecurity: false },
        nodeIntegration: false,
    })

    mainWindow.loadURL('http://localhost:3001');

    // mainWindow.setFullScreen(true)

    // mainWindow.setMenu(null);

    mainWindow.webContents.openDevTools()

    mainWindow.on('closed', function () {
        mainWindow = null
    })

    console.log('Electron window ready')
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
    app.quit()
})

core.start()

It seems you didn't install Electron globally, for that you need to run npm install -g Electron 似乎您没有全局安装Electron,因为您需要运行npm install -g Electron

Replace mainWindow.loadURL('http://localhost:3001'); 替换mainWindow.loadURL('http://localhost:3001'); With: 附:

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

You have not shared your package.json file, but I will guess you did not run npm install --save electron in your terminal. 您尚未共享package.json文件,但我想您可能没有运行npm install --save electron在终端中保存npm install --save electron

Also, instead of: 另外,代替:

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

you want to write it like so: 您想这样写:

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

I would review ES6 destructuring and unless you did not share the code with us, you should start your electron project by assuring that the app object is ready and loading your file like so: 我将回顾一下ES6的销毁工作,除非您不与我们共享代码,否则应通过确保app对象已准备就绪并加载文件来启动电子项目,如下所示:

let mainWindow;

app.on('ready', () => {
  mainWindow = new BrowserWindow({});
  mainWindow.loadURL(`file://${__dirname}/main.html`);
});

You will notice I declared an empty mainWindow variable to take care of any scoping issues you may have as you may have to use mainWindow in other functions as well. 您会注意到我声明了一个空的mainWindow变量,以解决可能遇到的范围问题,因为您可能还必须在其他函数中使用mainWindow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM