成角度-模型(模型接口)的声明如下:
export interface PersonInterface {
id: number;
name: string;
}
,然后在其他组件,服务等中,您导入此接口并按如下方式使用它:
...
import { PersonInterface } from './blabla';
...
export class SomeService {
person: PersonInterface;
}
所以我的问题是-在接口的声明中使用export
的目的是什么?
interface PersonInterface {
id: number;
name: string;
}
如果我不使用导出-我仍然可以在服务中使用它,甚至不需要导入它:
export class SomeService {
person: PersonInterface;
}
一切正常,没有错误,我可以从IDE中查看接口的声明(Ctrl +单击接口名称)。我想念什么吗?
欢呼
答案 0 :(得分:0)
这涉及到javascript历史的悠久历史。在早期,您必须使用<script src="name.js">
标记按从上到下的确切顺序手动指定javascript文件。附加脚本后,所有{em {1}}或var a = 5
之类的声明将可用于其下 以及全局的所有脚本。很难从全局范围中隐藏这些声明来实现封装,因此有必要创建一些新的东西。
后来function util(){}
到达了。为了使用其他脚本中的某些功能,您必须在该脚本的结尾处es modules
。这意味着所有其他未导出的声明(函数和变量)已从其他脚本中被封装(隐藏)。这样可以使脚本更具弹性,并且可以防止意外修改(假设某人第一次使用一堆脚本,分析10个以上的脚本而没有任何文档将是一场噩梦)。带有export
声明的脚本如下:
export
在2012年代创建了打字稿。除了静态功能,适当的类型检查和语法糖之类的漂亮功能之外,它还为var a = 5;
function b(){};
module.exports = {
a: a,
b: b
}
函数,变量和类提供了更好的方法。语法变为:
export/import
用于默认导出import Something from '../other.script'
// other.script.ts contents
export default const A = '5'; // this will be imported in other file
用于非默认导出import {exactOne, exactTwo} from '../other.script'
// other.script.ts contents
export const exactOne = 1;
export const exactTwo = 2;
export const exactThree = 3; // this is NOT imported above, but it's possible to import this const.
const exactFour = 4; // this const cannot be imported at all
用于批量导入。import * as AllTogether from '../other.script'
希望这对您有帮助!