gpt4 book ai didi

electron - ejs-electron 不能与 Electron 锻造一起使用

转载 作者:行者123 更新时间:2023-12-03 12:35:11 30 4
gpt4 key购买 nike

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

electron-forge init ejs-test

我正在使用 Electron 锻造 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 开始

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

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

你能帮我吗?

做更多调查,我发现 ejs-electron 中的关键线
protocol.interceptBufferProtocol('file', protocolListener)

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

最佳答案

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

我需要使用 ejs 的原因是为了根据多语言的消息文件动态生成我的 html 代码。我首先尝试动态生成它并将 loadURL 与 'data:text/html' 协议(protocol)一起使用。但是,这会产生很多问题,因为 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 文件几乎是样板 Electron 锻造:
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)
})

关于electron - ejs-electron 不能与 Electron 锻造一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52354893/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com