【问题标题】:ejs-electron not working with electron-forgeejs-electron 不能与电子锻造一起使用
【发布时间】:2019-02-20 14:52:42
【问题描述】:

在我的 electron-forge 应用程序中,在 Windows 10 上运行,ejs 模板未呈现,尽管没有可见错误。 我可以使用使用

创建的应用程序重现该问题
electron-forge init ejs-test

我正在使用 electron-forge 5.2.2 和 ejs-electron 2.03 这是我的 index.js 文件:

import { app, BrowserWindow } from 'electron'

import * as ejse from 'ejs-electron'

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit()
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

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

  ejse.data('testdata', 'Generated throuh EJS')
  // and load the index.html of the app.
  mainWindow.loadFile(`${__dirname}/index.ejs`)

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

  // Emitted when the window is closed.
  mainWindow.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.
    mainWindow = 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 OS X 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 OS X 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 (mainWindow === null) {
    createWindow()
  }
})

这是我的 index.ejs 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <%= testdata %>
  </body>
</html>

当我运行它时 npm 开始

我没有收到任何错误,但 按原样显示,而不是被替换。 在加载我的 ejs 文件之前调用 ejse.listening() 返回 true。

相同的代码适用于非伪造电子应用程序。

你能帮忙吗?

做了一些调查,我发现 ejs-electron 中的关键行

protocol.interceptBufferProtocol('file', protocolListener)

返回以下错误: 错误:方案已被拦截

【问题讨论】:

    标签: electron ejs electron-forge


    【解决方案1】:

    这与https://github.com/electron-userland/electron-forge/issues/291 的问题相同。 鉴于目前缺乏文档,升级到 electron-forge v6 太难了,所以我找到了解决方法。

    我需要使用 ejs 的原因是为了基于多语言的消息文件动态生成我的 html 代码。我首先尝试动态生成它并将 loadURL 与 'data:text/html' 协议一起使用。但是,这会产生很多问题,因为 html 与文件路径没有关联,因此任何引用都没有

    所以我转而在构建时为每种语言生成一个 html 文件,并在运行时根据语言选择要加载的文件。

    所以两个步骤: 我在 package.json 中添加了以下内容:

      "scripts": {
        "build" : "node build.js"
      },
    

    build.js 是:

    const ejs = require('ejs')
    const fs = require('fs')
    let langs = ['en', 'fr']
    langs.forEach((lang) => {
      let msgdata = JSON.parse(fs.readFileSync(`${__dirname}/src/messages/${lang}.json`, 'utf8'))
    
      ejs.renderFile(`${__dirname}/src/views/pages/mindmap.ejs`,
        {
          'messages': msgdata,
          'otherlanguage': 'fr',
        },
        {},
        (err, str) => {
          fs.writeFileSync(`${__dirname}/src/mindmap-${lang}.html`, str, 'utf8')
        })
    })
    

    因此,npm run build 会生成与语言文件一样多的 html 文件。

    然后我的应用程序的 index.js 文件几乎就是样板电子锻造:

    const { app, BrowserWindow, ipcMain } = require('electron')
    const ejs = require('ejs')
    const fs = require('fs')
    
    // Handle creating/removing shortcuts on Windows when installing/uninstalling.
    if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
      app.quit()
    }
    
    // Keep a global reference of the window object, if you don't, the window will
    // be closed automatically when the JavaScript object is garbage collected.
    let mainWindow
    
    const createWindow = () => {
      // Create the browser window.
      mainWindow = new BrowserWindow()
    
      // and load the index.html of the app.
      mainWindow.loadURL(`file://${__dirname}/mindmap-${lang}.html`)
    
      // Emitted when the window is closed.
      mainWindow.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.
        mainWindow = 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 OS X 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 OS X 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 (mainWindow === null) {
        createWindow()
      }
    })
    
    // In this file you can include the rest of your app's specific main process
    // code. You can also put them in separate files and import them here.
    ipcMain.on('request-messages', (event, arg) => {
      mainWindow.webContents.send('messages', msgdata)
    })
    

    【讨论】:

    • 这个问题的骄傲所有者:D
    • 还在答案中发布你是如何做到的(我的意思是代码)
    猜你喜欢
    • 2019-05-01
    • 2020-10-10
    • 1970-01-01
    • 2020-07-05
    • 2021-08-09
    • 2019-07-15
    • 2020-04-08
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多