I want to open 'file1.ts' and write:
我想打开'file1.ts'并写:
export var arr = [1,2,3];
and open another file, let's say 'file2.ts' and access directly to 'arr' in file1.ts:
并打开另一个文件,让我们说'file2.ts'并直接访问file1.ts中的'arr':
I do it by:
我是这样做的:
import {arr} from './file1';
However, when I want to access 'arr', I can't just write 'arr', but I have to write 'arr.arr'. The first one is for the module name. How do I access directly an exported variable name?
但是,当我想访问'arr'时,我不能只写'arr',但我必须写'arr.arr'。第一个是模块名称。如何直接访问导出的变量名称?
2 个解决方案
#1
29
If you do:
如果你这样做:
var arr = [1,2,3];
export default arr;
...
...
import arr from './file1';
Then it should work
然后它应该工作
#2
10
There are two different types of export, named and default.
有两种不同类型的导出,名为default和default。
You can have multiple named exports per module but only one default export.
每个模块可以有多个命名导出但只有一个默认导出。
For a named export you can try something like...
对于命名导出,您可以尝试类似...
// ./file1.ts
let arr = [1,2,3];
export { arr };
then to import you could use the same statement
然后导入你可以使用相同的语句
// ./file2
import { arr } from "./file1";
console.log(arr.length);
This will get around the need for arr.arr
you mentioned
这将解决您提到的arr.arr的需求
#1
29
If you do:
如果你这样做:
var arr = [1,2,3];
export default arr;
...
...
import arr from './file1';
Then it should work
然后它应该工作
#2
10
There are two different types of export, named and default.
有两种不同类型的导出,名为default和default。
You can have multiple named exports per module but only one default export.
每个模块可以有多个命名导出但只有一个默认导出。
For a named export you can try something like...
对于命名导出,您可以尝试类似...
// ./file1.ts
let arr = [1,2,3];
export { arr };
then to import you could use the same statement
然后导入你可以使用相同的语句
// ./file2
import { arr } from "./file1";
console.log(arr.length);
This will get around the need for arr.arr
you mentioned
这将解决您提到的arr.arr的需求