javascript - Angular:导出类和公共(public)类之间的区别?

标签 javascript angular typescript angular8 javascript-framework

在 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/

相关文章:

javascript - 如何从对象数组中获取特定数据?

javascript - 使用 FusionTables 层关闭 Google v3 API InfoWindow

javascript - 如何在React上正确开发分页?

javascript - 如何在 Windows 8 应用程序中显示数组中的数据

javascript - chrome 扩展中未定义 jquery

javascript - 如何在 Angular 2中设置或删除当前路由中的queryParams而不重新加载页面

javascript - 使用 Javascript 客户端库作为 Angular 2 服务的 Google API 端点

javascript - 如何在 Angular 7 中调整 Angular Material 对话框的大小?

javascript - Angular 4 http响应混合在一起

angular - 如何仅在 primeNG 数据表 Angular 2 过滤器中的按键输入时触发