简体   繁体   English

Angular 2中的组件是什么

[英]What are Components in Angular 2

I am new to Angular thing and have very little experience with Angular JS 1.x generation. 我是Angular的新手,对Angular JS 1.x的生成经验很少。 However my question is about Angular 2. I was reading about Components here and https://angular.io/docs/ts/latest/guide/architecture.html 但是我的问题是关于Angular 2的。我在这里https://angular.io/docs/ts/latest/guide/architecture.html阅读有关Components 信息。

I am using TypeScript and my question is : Is it safe to say that Component is a class (NOT @component annotation) similar to Model (as in Asp.Net MVC), since we can bind html controls with the fields defined in component class OR is it more like controller ? 我正在使用TypeScript,我的问题是:可以肯定地说Component是一个类似于Model的类(不是@component注释)(就像在Asp.Net MVC中一样),因为我们可以将html控件与组件类中定义的字段绑定在一起还是更像控制器? Or there is more which I am missing ? 还是我想念的还有更多?

There is a statement in 2nd Url, which says: 在第二个网址中有一条声明,内容为:

We define a Component's application logic - what it does to support the view - inside a class 我们在类中定义了组件的应用程序逻辑-它支持视图的作用

Above statement is increasing my confusion, because we can do a lot of things inside a class which is bound with html. 上面的声明增加了我的困惑,因为我们可以在与html绑定的类中做很多事情。 On text change we can remotely check something or on button click we can call a method and all this can be defined in a component class. 在更改文本时,我们可以远程检查某些内容,或者在单击按钮时,我们可以调用方法,所有这些都可以在组件类中定义。 So what exactly is the restriction of Components ? 那么Components的限制到底是什么? Can we treat them like models or like controllers or both ? 我们可以将它们像模型或控制器一样对待吗?

Please help me in clarifying this 请帮我澄清一下

In fact, the component class corresponds to your component implementation. 实际上,组件类与您的组件实现相对应。 I mean your own processing: 我的意思是您自己处理:

  • Properties correspond to component state. 属性对应于组件状态。 The state can be bound to the associated template if wich. 可以将状态绑定到关联的模板。
  • Methods correspond to processing your can trigger from the view or use internally in the component. 方法对应于可以从视图触发或在组件内部使用的处理。 Some methods correspond to hooks for the component lifecycle (see https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html ). 一些方法对应于组件生命周期的挂钩(请参阅https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html )。

So the component class can be seen as a mix of Angular1 controllers and scopes. 因此,组件类可以看作是Angular1控制器和范围的混合体。

The @Component decorator is what will make the component part of Angular2. @Component装饰器将使组件成为Angular2的一部分。 I mean involved in the different features and mechanims of the framework and your application. 我的意思是涉及框架和应用程序的不同功能和机制。

It will make possible to configure the component with different things to quote a few: 可以使用不同的内容配置组件,以引用一些内容:

  • a selector 选择器
  • a template 模板
  • inputs and outputs (can be also configured using @Input and @Ouput 输入和输出(也可以使用@Input@Ouput进行配置
  • specific providers 具体提供者
  • directives / components to use in the component 组件中使用的指令/组件
  • pipes to use in the component 组件中使用的管道

Moreover you can see a class decorator (the @Component decorator is of this type) as a kind of interceptor: 此外,您可以看到类装饰器( @Component装饰器是这种类型)作为一种拦截器:

  • It will make possible the dependency injection of parameters of the component constructor. 这将使组件构造函数的参数依赖注入成为可能。
  • It will make the component instance part of the change detection leveraging ZoneJS. 利用ZoneJS,它将使组件实例成为更改检测的一部分。 Mark's gave an awesome explain on this: What is the Angular2 equivalent to an AngularJS $watch? 马克对此做了一个很棒的解释: Angular2等效于AngularJS $ watch? .
  • It will add metadata on the component instance according to what was configured using Reflect-Metadata. 它将根据使用Reflect-Metadata配置的内容在组件实例上添加元数据。

So the @Component decorator is really important to configure the component and make it part of Angular2 mechanisms. 因此, @Component Component装饰器对于配置组件并使之成为Angular2机制的一部分非常重要。

Note for everyone : I tried to describe this simply and this corresponds to my understanding of things but feel free to comment my answer ;-) 给大家的注释 :我试图简单地描述它,这与我对事物的理解相对应,但是随时可以评论我的答案;-)

Directives/components replace the mix of controllers, scopes, and directives from AngularJS (Angular 1). 指令/组件取代了AngularJS(Angular 1)中的控制器,范围和指令的混合。

A component is a view and an associated view controller. 组件是视图和关联的视图控制器。 (Directives don't have views.) Components are how we create views for our application and support those views with (minimal) state, data, and logic. (指令没有视图。)组件是我们为应用程序创建视图并使用(最小)状态,数据和逻辑支持这些视图的方式。

A view is an HTML template with some additional Angular syntax that controls an area of the screen/display. 视图是带有一些其他Angular语法的HTML模板,用于控制屏幕/显示区域。 The component exposes some (subset!) of the application data to a view, and it handles the UI logic for the view. 该组件向视图公开一些(子集!)应用程序数据,并且它处理视图的UI逻辑。 Data should not be owned by components. 数据不应归组件所有。 Rather the component should get references to only the data it needs to drive the view. 而是该组件应该仅引用驱动视图所需的数据。 (This is similar to the same best practice used in AngularJS -- controllers should get references to data, not own it.) Services should normally own data. (这类似于AngularJS中使用的最佳实践-控制器应该获取对数据的引用,而不是拥有它。)服务通常应该拥有数据。

Similarly, component logic should be limited to the logic needed to drive the view (hence, "view logic"). 同样,组件逻辑应限于驱动视图所需的逻辑(因此称为“视图逻辑”)。 Application logic belongs in services. 应用程序逻辑属于服务。 Other tasks also belong in services and not in components: validating user input, logging, interacting with (web) servers, etc. 其他任务也属于服务而不属于组件:验证用户输入,日志记录,与(Web)服务器交互等。

So, components (like AngularJS controllers) should be as "thin" as possible. 因此,组件(如AngularJS控制器)应尽可能“薄”。 They should handle user interaction and define the data bindings required for such. 他们应处理用户交互并定义为此所需的数据绑定。 They should be focused on supporting the view. 他们应该集中精力支持这种观点。

Angular creates and destroys components as necessary as the user interacts with the application. 当用户与应用程序交互时,Angular会根据需要创建和销毁组件。 Components have a lifecycle, and there are lifecycle hooks that we can tap into. 组件具有生命周期,我们可以利用生命周期挂钩。

A component is just a class until we tell Angular about it. 在我们向Angular讲述之前,组件只是一个类。 And we do that by attaching metadata to the class. 我们通过将元数据附加到类上来实现。

I find it more important to know what belongs in a component and what doesn't, rather than trying to determine if it is a "controller" or a "model" -- those terms are so broad and overused that I don't think you can get two developers to agree on a definition of either term. 我发现更重要的是要知道组件中的内容,而不是组件中的内容,而不是试图确定它是“控制器”还是“模型”-这些术语如此广泛和过度使用,我认为您可以让两个开发人员就任一术语的定义达成共识。

Some of the above sentences are likely copied from the Angular.io docs, blogs, other SO posts, etc. I have a bunch of notes about Angular in a document, and I don't always keep track of source references. 上面的某些句子很可能是从Angular.io文档,博客,其他SO帖子等中复制的。我在文档中有很多关于Angular的注释,但我并不总是跟踪源引用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM