[英]How to integrate and run existing ReactJS Application in Electron
例如,我有一个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");
});
但这给我的./app.js
import React from 'React'
行import React from 'React'
带来了一些错误。
因此,我认为,我只应加载包含已编译的bundle.js
./public/index.html
文件。 但我想知道,这行app.on("ready", function(){
期望有一个app
app.on("ready", function(){
将如何工作。
此外,我也尝试在./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();
}
});
基本上,这很简单。 电子的作用就像台式铬包装纸一样,在台式铬内显示您任何(任何)类型的网页。
因此,例如,我们要显示http://www.google.com,然后您只需将此URL传递给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应用程序。 它不编译,不充当服务器。 它只是一个盒子,您可以在其中放置任何东西,并赋予其桌面外观,例如菜单,桌面通知,本地文件系统访问权限等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.