导出要在另一个JavaScript文件中使用的数组 [英] Export array to be used in another javascript file
问题描述
在我的帖子请求中,我想检查 thisString
是否在另一个javascript文件数组中存在。
In my post request, I want to check if thisString
exists in another javascript file array.
Array.js
exports.names = [
'John',
'Mary'
]
Main.js
if (names.includes(thisString)) {
...do stuff...
}
如果 thisString = Mary
, Main.js
返回未定义。如果我 console.log(names)
,它将返回数组。但是 console.log(names [0])
是未定义的。如果我将数组复制并粘贴到 Main.js
文件中,它将按预期工作。
if thisString = Mary
, Main.js
returns "undefined". If I console.log(names)
it returns the array. But console.log(names[0])
is undefined. And If I copy and paste the array into the Main.js
file it works as intended.
我想将数组放在另一个文件中只是为了清理一点。但是我在做什么错呢?
I want to have the array in another file just to clean things up a bit. But what am I doing wrong?
推荐答案
Typescript:
Typescript :
Array .ts
Array.ts
export let array = [1,2,3];
Main.ts
import {array} from "./Array.ts"
array.map(item => console.log(item +1))
JavaScript(nodejs)
Javascript (nodejs)
array.js
exports.array = [1,2,3];
main.js
let module = require('./array.js');
let array = module.array;
array.map(item => console.log(item +1))
这篇关于导出要在另一个JavaScript文件中使用的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!