简体   繁体   English

Angular 2:为什么我需要像Hero.ts这样的课程?

[英]Angular 2: Why do I need classes like Hero.ts?

I am a web developer coming from Angular 1.3. 我是来自Angular 1.3的Web开发人员。 Now I'm learning Angular 2 and there is one thing I can't get clear: 现在我正在学习Angular 2,有一点我无法理解:

When I have a page where I can view heroes, I would have a controller and a service to get the data eg from a REST-API in Angular1. 当我有一个可以查看英雄的页面时,我会有一个控制器和一个服务来从Angular1中的REST-API获取数据。

In Angular2 I have the component, a service and a Hero class . 在Angular2中,我有组件,服务和Hero类 Why do I need that class 'Hero'? 为什么我需要那个班级'英雄'?

What are the advantages in comparison to just using objects like in Angular1? 与仅使用Angular1中的对象相比,有什么优势? Is it best practice to implement a class for every object I use in my application? 最好的做法是为我在我的应用程序中使用的每个对象实现一个类吗?

Thanks a lot! 非常感谢!

If it's the Hero class from https://angular.io/docs/ts/latest/tutorial/toh-pt1.html then it's just the data the component is working with (display, edit, ...) 如果它是来自https://angular.io/docs/ts/latest/tutorial/toh-pt1.htmlHero类,则它只是组件正在使用的数据(显示,编辑,......)

As mentioned in the comments. 正如评论中所提到的那样。 If concrete classes are used you get full benefit of the static type checking capabilities of TypeScript and related tools. 如果使用具体的类,您可以充分利用TypeScript和相关工具的静态类型检查功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular2英雄之旅-Hero.ts属性和嵌套对象 - Angular2 Tour of Heroes - Hero.ts Property & Nested Objects 文件 'app/hero.ts' 不是控制台中的模块错误,在哪里使用 angular2 将接口文件存储在目录结构中? - File 'app/hero.ts' is not a module error in the console, where to store interfaces files in directory structure with angular2? 我是否需要创建一个 Angular 库来发布一个引用 Angular 类的简单 TS 类 - Do I need to create an Angular library to publish a simple TS class that references Angular classes 我在Angular 2中需要app.component.ts吗? - Do I need app.component.ts in Angular 2? 为什么我需要 *public-api.ts* 和 *exports* - Why do i need *public-api.ts* and also *exports* 为什么我需要标记检查? angular - Why do I need mark for check ? angular 为什么我需要添加 markForCheck() 来触发 Angular 中的更改检测? - Why do i need to add markForCheck() to trigger change detection in Angular? 为什么我需要HTTP服务器来运行Angular 2? - Why do I need a HTTP-server to run Angular 2? 我需要关闭angular4中component.ts中的弹出消息 - I need to close the popup message in component.ts in angular4 breeze-brideg-angular2:我是否需要创建我的微风实体类 - breeze-brideg-angular2 : do I need to create my breeze entity classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM