// main.js
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
},
});
// Load the Vue app
win.loadURL('http://localhost:8080'); // Assuming Vue dev server is running on port 8080
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// preload.js
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector);
if (element) element.innerText = text;
};
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type]);
}
});
// package.json
{
"name": "electron-vue-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron": "electron ."
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"electron": "^13.1.7",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"vue-template-compiler": "^2.6.11"
}
}
// Vue App (src/App.vue)
<template>
<div id="app">
<h1>Hello Electron + Vue!</h1>
</div>
</template>
<script>
export default {
name: 'App',
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
main.js: 这是 Electron 的主进程文件,负责创建和管理浏览器窗口。它加载了 Vue 应用程序的开发服务器(假设在 http://localhost:8080
上运行)。
preload.js: 这是一个预加载脚本,允许你在渲染进程中访问 Node.js API 和其他受限功能。这里只是一个简单的示例,用于显示 Electron、Node.js 和 Chrome 的版本信息。
package.json: 包含项目依赖项和脚本。"start"
脚本用于启动 Vue 开发服务器,而 "electron"
脚本用于启动 Electron 应用程序。
Vue App (src/App.vue): 这是 Vue 应用程序的入口文件,包含了一个简单的模板、脚本和样式。
通过这些文件,你可以创建一个使用 Electron 和 Vue 构建的桌面应用程序。确保你已经安装了所有必要的依赖项,并且 Vue 开发服务器正在运行,然后使用 npm run electron
启动 Electron 应用程序。
上一篇:vue3 深度监听
下一篇:vue 异步请求
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站