ejs electron不使用electron forge

ejs electron不使用electron forge,electron,ejs,electron-forge,Electron,Ejs,Electron Forge,在我的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'

在我的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>
正在返回以下错误: 错误:方案已被拦截

这与问题相同。 鉴于目前缺乏文档,升级到electron forge v6太困难了,所以我找到了一个解决办法

我需要使用ejs的原因是基于多种语言的消息文件动态生成html代码。我首先尝试动态生成它,并使用loadURL和“data:text/html”协议。但是,这会产生很多问题,因为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运行构建生成的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)
})

本期自豪的拥有者:Dalso在回答中发布了你是如何做到这一点的(我指的是代码)
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)
})