[英]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.html的Hero
类,则它只是组件正在使用的数据(显示,编辑,......)
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.