我可以从Chrome加载http://localhost:8080
,但是win.loadURL('http://localhost:8080/');
出现错误,我不知道原因。
答案 0 :(得分:2)
我的代码
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 load() {
mainWindow.loadURL('http://127.0.0.1:8080');
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadURL('http://127.0.0.1:8080');
mainWindow.loadFile('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
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
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()
}
});
// In this file you can include the rest of your app's specific main process
这是 package.json
{
"version": "1.0.0",
"name": "idena-pocket",
"private": true,
"main": "public/main.js",
"scripts": {
"dev": "export NODE_ENV=development && webpack-dev-server --https --host 127.0.0.1 --open",
"electron-dev": "concurrently \"BROWSER=none npm run dev\" \"electron .\"",
"electron-dev1": "concurrently \"BROWSER=none\" \"electron .\"",
"electron-dev2": "concurrently \"BROWSER=none npm run dev\" \"wait-on https://10.0.2.2:8080/ && electron .\"",
"electron-package": "./node_modules/.bin/electron-builder -c.extraMetadata.main=build/start-electron.js",
"preelectron-package": "npm run build",
"i18-scanner": "i18next-scanner src/**/*.{js,jsx,ts,tsx,html}",
"format": "prettier-standard --format"
},
"dependencies": {
"react-scripts": "0.8.5",
"bip39": "^3.0.2",
"concurrently": "^6.0.2",
"connected-react-router": "^6.8.0",
"crypto-js": "^4.0.0",
"electron": "^12.0.6",
"electron-builder": "^22.11.1",
"electron-is-dev": "^2.0.0",
"history": "^4.10.1",
"i18next": "^19.4.4",
"i18next-browser-languagedetector": "^4.1.1",
"i18next-scanner": "^2.11.0",
"idena-js": "1.1.5",
"react": "^16.13.1",
"react-copy-to-clipboard": "^5.0.2",
"react-dom": "^16.13.1",
"react-hook-form": "^5.5.3",
"react-i18next": "^11.4.0",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-tooltip": "^4.2.5",
"redux": "^4.0.5",
"styled-components": "^5.0.1",
"sweetalert": "^2.1.2",
"wait-on": "^5.3.0"
},
"devDependencies": {
"@types/react": "^16.9.25",
"@types/react-dom": "^16.9.5",
"@types/react-redux": "^7.1.7",
"@types/react-router-dom": "^5.1.3",
"@types/styled-components": "^5.1.0",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^5.1.1",
"css-loader": "^3.4.2",
"file-loader": "^6.0.0",
"html-webpack-plugin": "^3.2.0",
"pre-commit": "^1.2.2",
"prettier-standard": "^16.3.0",
"react-hot-loader": "^4.12.20",
"ts-loader": "^6.2.1",
"typescript": "^3.8.3",
"url-loader": "^4.1.0",
"webpack": "^4.43.0",
"webpack-bundle-analyzer": "^3.7.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0",
"workbox-webpack-plugin": "^5.1.3"
},
"pre-commit": [
"format"
]
}