问题

安装Electron: 1.先确保安装了Node.js,可以在命令行输入node -v检查是否安装成功。 2.创建一个空…

安装Electron:

1.先确保安装了Node.js,可以在命令行输入node -v检查是否安装成功。

2.创建一个空文件夹,进入该文件夹。

3.在命令行输入npm init -y,生成一个package.json文件。

4.在命令行输入npm install electron –save-dev,安装Electron。

创建示例Demo:

1.进入刚才创建的空文件夹,新建一个index.html文件,写入如下代码:

“`

Hello World!

Hello World!

We are using Electron.

“`

2.在同一目录下新建一个main.js文件,写入如下代码:

“`
const {app, BrowserWindow} = require(‘electron’)
const path = require(‘path’)
const url = require(‘url’)

let win

function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, ‘index.html’),
protocol: ‘file:’,
slashes: true
}))

// Open the DevTools.
//win.webContents.openDevTools()

// Emitted when the window is closed.
win.on(‘closed’, () => {
// 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.
win = 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’, () => {
// On macOS 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’, () => {
// On macOS 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 (win === null) {
createWindow()
}
})
“`

3.在命令行输入node main.js启动应用程序,可以看到一个新窗口弹出,显示“Hello World! We are using Electron.”。

以上就是Electron的安装和使用以及一个简单Demo的示例。

个人中心
今日签到
有新私信 私信列表
搜索