網(wǎng)站首頁(yè) 編程語言 正文
一、創(chuàng)建react項(xiàng)目
首先使用creat-react-app腳手架來創(chuàng)建一個(gè)react項(xiàng)目
# 安裝 create-react-app 命令,如果已將安裝請(qǐng)忽略
npm install -g create-react-app
# 創(chuàng)建 react項(xiàng)目
create-react-app react-electron
# 啟動(dòng)項(xiàng)目( create-react-app 真的超級(jí)方便啊)
cd react-electron && npm start
npm start之后瀏覽器會(huì)自動(dòng)打開網(wǎng)址 http://localhost:3000/ ,出現(xiàn)react項(xiàng)目的頁(yè)面了
二、安裝Electron
#在react-electron項(xiàng)目目錄下
npm install -save electron
三、配置文件
1.在項(xiàng)目根目錄新建main.js
const { app, BrowserWindow } = require("electron")
const path = require('path')
const url = require('url')
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 800,
minWidth: 800,
minHeight: 800
})
win.loadURL("http://localhost:3000/")
}
app.whenReady().then(() => {
createWindow()
app.on("active", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})
2.配置package.json
添加如下兩項(xiàng)配置:
{
"main": "main.js", // 配置electron的啟動(dòng)文件
"scripts": {
"electron-start": "electron ."
},
}
3.啟動(dòng)electron
# 這里要打開兩個(gè)窗口
# 啟動(dòng)react項(xiàng)目
npm start
# 啟動(dòng)electron
npm run electron-start
npm run electron-start之后會(huì)自動(dòng)出現(xiàn)桌面應(yīng)用
四、打包Electron桌面應(yīng)用
1.安裝 electron-packager
npm install electron-packager -D
2.** 配置homePage字段**
在package.json文件中添加如下配置
{ "homepage": "." }
{ "script": { "package": "electron-packager ./build react-electron --platform=darwin --arch=x64 --electron-version 17.1.0 --overwrite" } } //mac系統(tǒng) --platform=darwin //Windows系統(tǒng) --platform=win32
然后打包react代碼,根目錄會(huì)多出一個(gè)build文件夾
npm run build
將main.js和package.json兩個(gè)文件復(fù)制到build文件夾下,同時(shí)修改build文件夾下的main.js
// 加載應(yīng)用----electron默認(rèn)的打包入口
win.loadURL(url.format({
pathname: path.join(__dirname, './index.html'), // 修改這里
protocol: 'file:',
slashes: true
}))
在根目錄運(yùn)行打包命令
npm run package
原文鏈接:https://blog.csdn.net/koufulong/article/details/128080185
相關(guān)推薦
- 2022-05-11 RabbitMq工作模式深度剖析與Spring整合MQ
- 2022-06-20 python中的隨機(jī)數(shù)?Random介紹_python
- 2022-10-15 Python基礎(chǔ)學(xué)習(xí)之函數(shù)和代碼復(fù)用詳解_python
- 2022-10-14 VS Code注釋插件doxygen documentation generator
- 2022-09-03 ASP.NET中Response.BufferOutput屬性的使用技巧_實(shí)用技巧
- 2022-03-19 C語言數(shù)據(jù)的存儲(chǔ)詳解_C 語言
- 2023-01-18 Go語言讀取YAML?配置文件的兩種方式分享_Golang
- 2022-03-26 在ASP.Net?Core應(yīng)用程序中使用Bootstrap4_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支