如何为单个导入添加别名?

时间:2018-12-17 15:27:13

标签: javascript typescript ecmascript-6

我正在使用自定义组件。在此组件中,我这样调用一个名为Card的子组件:

import { Card } from "./Card";

我正在这样使用它:

<Card></Card>

一切都很好。现在这是问题所在。这个自定义组件,我想使用文件“ ../../App/Models/Something”中的枚举。该枚举名称为Card。我知道我会写

import * as Something from "../../App/Models/Something";

然后在我的组件中执行Something.Card,但是“某事”包含很多我不需要的东西。如何为单个导入添加别名?

我尝试了这个,但是都没有用:

import { Card } as Something from "../../App/Models/Something";
import Card as Something from "../../App/Models/Something";

1 个答案:

答案 0 :(得分:3)

您几乎明白了!导入单个别名的正确方法是:

import { Card as Something } from "../../App/Models/Something";

阅读有关在MDN上导入的文档:

语法:

import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import("module-name");

解决您问题的人是import { export1 , export2 as alias2 , [...] } from "module-name"; Read more here