![](/img/trans.png)
[英]Document is not defined on node.js - How can i import a variable from another .js file to node.js server?
[英]How to refer a variable in Node.js from another file
我正在使用 node.js 构建一个应用程序,其中代码中有很多 static 文本(可能会在几个月内更改)。 我想将文本移动到单独的文件中,并将该文件数据作为处理程序文件中的变量引用。
例如
const result = await client.views.open({
view: {
type: 'new',
text: [{
text: {
type: "plain_text",
text: "One",
emoji: true
},
value: "One"
},
{
text: {
type: "plain_text",
text: "Two",
emoji: true
},
value: "Two"
}
]
}
});
} catch (error) {
console.error(error);
}
以上是原始文件代码。 我想要做的是将下面的代码移动到一个单独的文件中:
[{
text: {
type: "plain_text",
text: "One",
emoji: true
},
value: "One"
},
{
text: {
type: "plain_text",
text: "Two",
emoji: true
},
value: "Two"
}
]
之后使用诸如require(./newfile.js)
类的东西将其称为处理程序文件中的变量。
我面临的问题是这不是一个有效的 JSON,而是一个具有 JSON 结构的 object,所以不确定如何解决这个问题。
在newfile.js
module.exports = [
{
text: {
type: "plain_text",
text: "One",
emoji: true
},
value: "One"
},
{
text: {
type: "plain_text",
text: "Two",
emoji: true
},
value: "Two"
}
]
然后将其导入原始文件:
const innerText = require('./newfile.js');
const result = await client.views.open({
view: {
type: 'new',
text: innerText,
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.