[英]What am I doing wrong in my module export from main.js?
Learning how to develop modules I'm trying to learn how to export one from main.js . 学习如何开发模块我正在尝试学习如何从main.js导出模块 。 On my renderer I can send it correctly to main with:
在渲染器上,我可以使用以下命令将其正确发送到main :
renderer.js: renderer.js:
let _object = {
foo: foo1,
bar: bar1,
}
ipcRenderer.send('channel', _object)
in main.js I can get this correctly: 在main.js中,我可以正确获取此信息:
ipcMain.on('channel', (e, res) => {
console.log(JSON.stringify(res))
console.log(typeof res)
})
however when I export the result
from main.js and try to bring it into another file I get an undefined
: 但是,当我从main.js导出
result
并尝试将其带入另一个文件时,我得到了一个undefined
:
main.js: main.js:
const foobar = require('./foobar')
ipcMain.on('channel', (e, res) => {
console.log(JSON.stringify(res))
console.log(typeof res)
module.exports.res = res
foobar.testing()
})
foobar.js: foobar.js:
const res = require('./main')
module.exports = {
testing: function(res) {
console.log(`Attempting console.log test: ${res}`)
console.log(res)
console.log(JSON.stringify(res))
}
}
terminal result: 最终结果:
Attempting console.log test: undefined
undefined
undefined
I've also tried to redefine the object in main.js : 我也尝试过在main.js中重新定义对象:
ipcMain.on('channel', (e, res) => {
module.exports = {
foo: foo,
bar: bar,
}
console.log(`Testing object ${res.foo}`)
foobar.testing()
})
My research of reference: 我的参考研究:
What a I doing wrong in my export of the result
in main.js so I can use it in a different file? 我在main.js中导出
result
时出现了什么错误,因此可以在其他文件中使用它?
My end goal is to learn how to be able to call res.foo
in foobar.js . 我的最终目标是学习如何能够在
res.foo
中调用res.foo 。
First of all, you have an argument res
in your testing
function, which shadows the import. 首先,在
testing
函数中有一个res
量,它掩盖了导入。 Second, the imported res
object is all exports from main
, which includes the res
you need - so you should print res.res
instead of the whole object. 二,进口
res
对象是从所有出口main
,其中包括res
,你需要-所以你应该打印res.res
而不是整个的对象。
Foobar.js: Foobar.js:
const res = require('./main')
module.exports = {
testing: function() {
console.log(`Attempting console.log test: ${res.res}`)
console.log(res.res)
console.log(JSON.stringify(res.res))
}
}
The last version (where you reassign module.exports
) would not work, because foobar
will still have the original exports, which was an empty object. 最后一个版本(在其中重新分配
module.exports
)将不起作用,因为foobar
仍将具有原始导出,这是一个空对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.