【发布时间】:2018-04-11 08:28:22
【问题描述】:
我在这里得到了一个doozie。在Electron's 主进程中,我需要在下面的函数中使用ipcMain 设置事件处理程序。这使 main.js 文件更加精简。一切都很顺利,直到我编写了一些验证代码来确保用户传入一个对象。为此,我一直使用typeof,从来没有遇到过问题。但在 Electron 中,我得到了:
主进程发生 JavaScript 错误 - TypeError: Cannot 分配给对象'#'的只读属性'exports'
代码:
const {ipcMain} = require('electron');
function ipcSetup() {
ipcMain.on('123', function(event, arg) {
// this blows chunks...
if(arg && typeof arg === 'object') {
console.log(`All good....`);
}
// and if you comment that out, this use of "typeof" does the same thing
console.log(typeof arg);
// and to eliminate 'arg' as the issue...
let a = 1;
console.log(typeof a); // expect 'number', get Exception
});
}
module.exports = ipcSetup;
我不知道 Electron 是否使用 Object.defineProperty 将 arg 设为只读,但 typeof 无论如何都不会在这里分配,我消除了 arg 所以这个错误没有意义。环境是 Node 8.2.1 上的 Electron 1.8.4,使用 electron-vue
【问题讨论】:
-
typeof语法在主线程中运行良好。这可能是上下文或模块导入方式的问题。仍在努力寻找答案。