facebook twitter hatena line email

「Electron/インストール」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(インストール)
(サンプル)
行20: 行20:
 
</pre>
 
</pre>
  
==サンプル==
+
==helloworldサンプル==
 
index.js
 
index.js
 
<pre>
 
<pre>
行54: 行54:
 
   </body>
 
   </body>
 
</html>
 
</html>
 +
</pre>
 +
==サンプル(os別に適切に閉じる)==
 +
windows,linuxでは、すべてのwindowを閉じると、アプリが閉じるように。
 +
macでは、すべてのwindowを閉じても、アプリは閉じないように。
 +
<pre>
 +
const { app, BrowserWindow } = require('electron')
 +
 +
const createWindow = () => {
 +
  const win = new BrowserWindow({
 +
    width: 800,
 +
    height: 600
 +
  })
 +
 +
  win.loadFile('index.html')
 +
}
 +
app.whenReady().then(() => {
 +
  createWindow()
 +
 +
  app.on('activate', () => {
 +
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
 +
  })
 +
})
 +
app.on('window-all-closed', () => {
 +
  if (process.platform !== 'darwin') app.quit()
 +
})
 
</pre>
 
</pre>

2023年12月21日 (木) 15:13時点における版

インストール

公式:https://www.electronjs.org/ja/docs/latest/tutorial/quick-start

mkdir my-electron-app && cd my-electron-app
npm init
npm install --save-dev electron

package.json に"start": "electron ." を追加

{
  "scripts": {
    "start": "electron ."
  }
}

空のindex.jsを追加

touch index.js

helloworldサンプル

index.js

const { app, BrowserWindow } = require('electron')

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600
  })

  win.loadFile('index.html')
}
app.whenReady().then(() => {
  createWindow()
})

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using Node.js <span id="node-version"></span>,
    Chromium <span id="chrome-version"></span>,
    and Electron <span id="electron-version"></span>.
  </body>
</html>

サンプル(os別に適切に閉じる)

windows,linuxでは、すべてのwindowを閉じると、アプリが閉じるように。 macでは、すべてのwindowを閉じても、アプリは閉じないように。

const { app, BrowserWindow } = require('electron')

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600
  })

  win.loadFile('index.html')
}
app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})