Electron+React开发桌面应用 之Electron环境快速配置 :脚手架

环境搭建

前提是安装好npm和nodejs,安装以下开发环境,若安装失败则尝试翻墙或者使用淘宝镜像cnmp

electron环境安装

npm install -g electron-prebuilt
npm install -g electron-packager
// create-react-app安装
npm install -g create-react-app
npm install -g react-scripts
## 创建demo

在要创建工程的目录下使用命令create-react-app projectName创建工程:

create-react-app desktop-demo

装electron以及增加启动命令:

使用命令npm install -save electron安装electron
在脚本里添加启动命令"electron-start": "electron ." 

创建Electron的启动文件main.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')
const pkg = require('./package.json') // 引用package.json 

// 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.

    if(pkg.DEV) { 
    mainWindow.loadURL("http://localhost:3000/")
} else { 
  mainWindow.loadURL(url.format({
    pathname:path.join(__dirname, './build/index.html'), 
    protocol:'file:', 
    slashes:true 
  }))
} 

    // 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()
    }
})

package.json文件修改

在React项目中的package.json文件中增加main字段,值为"main.js",几homepage字段 "homepage":"."

如下:

{
  "name": "desktop-demo",
  "version": "0.1.0",
  "private": true,  //新增加
  "DEV":false,
  "main": "main.js", //新增加
  "homepage": ".", //新增加
  "dependencies": {
    "electron": "^1.7.9",
    "npm": "^5.4.2",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-scripts": "1.0.14"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "electron-start": "electron ."
  }
}

 

阅读更多

没有更多推荐了,返回首页