gpt4 book ai didi

node.js - 如何在 Electron 中集成和运行现有的 ReactJS 应用程序

转载 作者:行者123 更新时间:2023-12-03 12:23:35 25 4
gpt4 key购买 nike

例如,我有一个 ReactJS 应用程序:https://iaya-664f3.firebaseapp.com/
您可以在 HTML 源代码中看到 bundle.js文件。

我尝试使用 Electron 将这个应用程序作为桌面应用程序运行,它应该在 chromium 窗口中启动这个 Web 应用程序,但它不工作。

以下是我的主要 React 应用程序文件 app.js坐在根目录中。然而编译的文件bundle.jsindex.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 以在 Chrome 中运行。
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();
}
});

最佳答案

基本上事情很简单。 Electron 就像桌面 Chrome package 器一样,在桌面 Chrome 内显示您的任何(任何)类型的网页。

例如,我们要显示 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 应用程序。它不编译,不充当服务器。它只是一个盒子,您可以在其中放置任何东西并使其具有桌面外观,例如菜单、桌面通知、本地文件系统访问等。

关于node.js - 如何在 Electron 中集成和运行现有的 ReactJS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com