例如,我有一个ReactJS应用程序:https://iaya-664f3.firebaseapp.com/
您可以在HTML源代码中看到bundle.js
文件。
我曾尝试使用Electron将此应用程序作为桌面应用程序运行,它应该在chrome窗口中启动此Web应用程序,但它无法正常工作。
app.js
,位于根目录中。但编译文件bundle.js
和index.html
位于./public/
目录中。./app.js
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory } from 'react-router';
import routes from './routes';
import {Provider} from "react-redux";
import { createStore, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import rootReducer from './reducers/index';
const store = applyMiddleware(ReduxPromise)(createStore)(rootReducer);
ReactDOM.render( <Provider store={store}>
<Router history={browserHistory} routes={routes} />
</Provider> ,
document.getElementById('react-app'));
./index.js
在这个文件中,我将我的应用程序嵌入到Electron中以运行铬。
var app = require("./app");
var BrowserWindow = require("browser-window");
// on electron has started up , booted up, everything loaded (Chromium ,goen, live)
app.on("ready", function(){
var mainWindow = new BrowserWindow({
width:800,
height:600
});
mainWindow.loadUrl("file://" + __dirname+ "/public/index.html");
});
但这会在import React from 'React'
./app.js
行中出现错误。
所以我假设,我应该只加载./public/index.html
文件来加载包含已编译的bundle.js
的文件。但我想知道,这将如何工作,因为app.on("ready", function(){
行期望app
。
此外,我还在 ./index.js
中尝试了以下方式,但它会出现其他一些错误。
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const url = require('url');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
/*mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public/index.html'),
protocol: 'file:',
slashes: true
}));*/
mainWindow.loadURL("file://" + __dirname+ "/public/index.html");
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
答案 0 :(得分:2)
基本上事情很简单。电子就像桌面铬包装一样,可以在桌面铬中显示任何(任何)类型的网页。
例如,我们想要显示http://www.google.com,然后您只需将此网址传递给loadURL()
功能。
以下是代码的工作副本(在问题中提出):
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
app.on('ready', function(){
var mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadURL("http://localhost:8080/"); // option1: (loading a local app running on a local server)
//mainWindow.loadURL("https://iaya-664f3.firebaseapp.com"); // option2: (loading external hosted app)
// loading developer tool for debugging
mainWindow.webContents.openDevTools();
});
我希望这会澄清许多人的混淆,他们是Electron
的新手。所以最后的话是Electron
只加载现有的和正在运行的Web应用程序。它不编译,不作为服务器。它只是一个盒子,你可以放置任何东西,并给它一个桌面的外观,例如menues,桌面通知,本地文件系统访问等。