在 Angular 中,导出类和公共(public)类有什么区别?
“Angular 导入/导出用于使一个模块的内容可在另一个模块中使用。那么这与公共(public)类有何不同?”
引用问题: What is the exact meaning of export keyword in Angular 2\TypeScript?
示例:
export class Product {
constructor(
public id?: number,
public name?: string,
public category?: string,
public description?: string,
public price?: number) { }
}
最佳答案
历史
ES6/ES2015(ECMA 脚本)在该语言中引入了原生模块系统。在 ES6 之前,JavaScript 应用程序使用 requirejs 等库来实现模块系统。
模块
类、函数、常量等可以从模块中导出,也可以导入到其他模块中。未导出的内容是模块内部的内容。
尽管 TypeScript 在 ES 2015 之前就有类似的概念,但该语言采用了 ES6 模块系统来保持一致性和标准。在这里阅读更多内容。 https://www.typescriptlang.org/docs/handbook/modules.html
without modules, yesteryear applications used "script" elements carefully ordered, so that something is declared first, only then used in next few files. Also something declared in a previous script file, is not overridden with a new variable.
类(class)
另一方面,类是一种面向对象的编程概念,封装了状态(字段)和行为(函数)。访问修饰符控制字段在类实例上的可用方式。公共(public)(默认)、私有(private)(类内部)和 protected (在类和派生类中可访问)。更多这里,https://www.typescriptlang.org/docs/handbook/classes.html#classes
摘要
简而言之,从模块导入类、创建实例、使用公共(public)方法和属性。我们还可以导入函数、常量、枚举等。它们可能是模块的一部分。可能无法提供像类那样的封装和抽象。
关于javascript - Angular:导出类和公共(public)类之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58159889/