【electron】electron-builder-start 加入自己的html

最简单的electron程序莫过于:https://github.com/electron/electron-quick-start.git

但是该程序缺乏打包过程,因此借助 electron-builder-start 来熟悉更佳:https://github.com/QDMarkMan/electron-builder-start.git

练习代码地址:https://github.com/SmileEricXin/electronPractice.git

在 electron-builder-start 中,并没有放置html,因此要借助 electron-builder-start 作为模板来熟悉electron,第一步就是加入自己的html。

function createMainWindow() {
  const window = new BrowserWindow()

  if (isDevelopment) {
    window.webContents.openDevTools()
  }

  // 修改的这里,把html放入static
  let indexPath = path.join(__static, '/html/index.html')
  console.log('indexPath:', indexPath)
  window.loadURL(formatUrl({
    pathname: indexPath,
    protocol: 'file',
    slashes: true
  }))

  window.on('closed', () => {
    mainWindow = null
  })

  window.webContents.on('devtools-opened', () => {
    window.focus()
    setImmediate(() => {
      window.focus()
    })
  })

  return window
}

接下来就是放html文件了,在 electron-builder-start 目录下建立static/html文件夹,放入代码加载的html即可。

<html>
  <body>
    <div>
      hello electron!!!
      2019
      2020
    </div>
  </body>
</html>

效果如图:

build 之后,在dist包也是能看到相应html的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章