使用 Node.js,如果我写成app.js
:
var commons = {
title: 'myTitle',
description: 'MyDesc',
menu: {
home: {
label: 'Home',
url: '/',
},
contacts: {
label: 'Contacts',
url: '/contacts'
}
}
}
console.log(commons);
我有这个输出...
{
title: 'myTitle',
description: 'MyDesc',
menu: {
home: {
label : 'Home',
url: '/'
},
contacts: {
label: 'Contacts',
url: '/contacts'
}
}
}
......它工作正常。但是,如果我app.js
要从另一个文件(在同一路径中)加载一个变量......
commons.js
:
exports.commons = {
title: 'myTitle',
description: 'MyDesc',
menu: {
home: {
label: 'Home',
url: '/',
},
contacts: {
label: 'Contacts',
url: '/contacts'
}
}
}
app.js
:
var commons = require('./commons');
console.log(commons);
我作为输出:
commons: {
{
title: 'myTitle',
description: 'MyDesc',
menu: {
home: [Object],
contacts: [Object]
}
}
}
为什么会这样?如何正确地跨两个文件传递变量?