在我在 Windows 10 上运行的 electron-forge 应用程序中,没有呈现 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 start 运行它时
我没有收到错误,但 <%= testdata %> 按原样显示,而不是被替换。在加载我的 ejs 文件之前调用 ejse.listening() 会返回 true。
相同的代码适用于非伪造电子应用程序。
你能帮我吗?
做更多调查,我发现 ejs-electron 中的关键线
protocol.interceptBufferProtocol('file', protocolListener)
返回以下错误:错误:该方案已被拦截