简体   繁体   English

如果装饰了角度2组件,它们的类型是什么?

[英]What is the type of angular 2 components if they are decorated?

I need to have a map of different angular components. 我需要一张不同角度分量的图。 This is the map: 这是地图:

  private modals: MapOf<any> = {
    addAttribute: AddAttributeModalComponent,
    editAttribute: EditAttributeModalComponent,
    requestUnit: RequestUnitModalComponent,
    requestAttribute: RequestAttributeModalComponent
  };

As you can see, I have a generic type MapOf. 如您所见,我有一个通用类型MapOf。 This is it: 就是这个:

export interface MapOf<T> {
  [key: string]: T;
}

Right now I am confused with < any >, because clearly this is a map of components. 现在,我对<any>感到困惑,因为显然这是组件映射。 But I don't know how to set this type and I couldn't find the answer. 但是我不知道如何设置此类型,也找不到答案。

ComponentRef doesn't fit here, because it asks for a reference anyway. ComponentRef在这里不适合,因为它仍然要求引用。 Type Component doesn't fit also, because my components are decorated and they contain some methods which are not present on 'pure' Component. 类型Component也不合适,因为我的组件已经过装饰,并且包含一些“纯”组件上不存在的方法。

Any ideas? 有任何想法吗?

Decorators don't change the type of the class. 装饰器不会更改类的类型。 If you just want to avoid using the type any , have each component class to extend the same abstract class or implement the same interface. 如果只想避免使用any类型,则让每个组件类扩展相同的抽象类或实现相同的接口。 Then you can use this new type as the generic one. 然后,您可以将此新类型用作通用类型。 For example, 例如,

class AddAttributeModalComponent implements MyInterface{...}

MapOf<MyInterface> = {...}

The other option to try would be to create a new union type: 尝试的另一种选择是创建一个新的联合类型:

type MyNewType = AddAttributeModalComponent | 
                 EditAttributeModalComponent |
                 RequestUnitModalComponent |
                 RequestAttributeModalComponent;

MapOf<MyNewType> = {...};

Short answer: There're no such thing like that. 简短的回答:没有这样的事情。

Long Answer: We define the Component by using the @Component({...}) decorator to provide some metadata about the Component . 长答案:我们通过使用@Component({...})装饰器定义Component ,以提供有关Component一些元数据。 Angular will use that metadata to create the View , which is a low-level abstraction of the Component we used to know. Angular将使用该元数据来创建View ,这是我们过去知道的Component的低级抽象。

So, the below Component interface for the @Component decorator is NOT the real "component class": 因此, @Component Component装饰器的以下Component接口不是真正的“组件类”:

import {Component} from '@angular/core';

To achieve what you're trying to do, I think Yakov Fain's answer is quite correct. 为了实现您想要的目标,我认为Yakov Fain的答案是正确的。 Either you can create an interface or an abstract class then implement/extend your component classes from there. 您可以创建一个接口或一个抽象类,然后从那里实现/扩展您的组件类。

If you want to know more about the view, there're some nice articles by Ng Wizard about this topic: https://blog.angularindepth.com/exploring-angular-dom-abstractions-80b3ebcfc02 https://blog.angularindepth.com/everything-you-need-to-know-about-change-detection-in-angular-8006c51d206f 如果您想了解更多关于看来,是由吴向导有关此主题的一些不错的文章: https://blog.angularindepth.com/exploring-angular-dom-abstractions-80b3ebcfc02 HTTPS://blog.angularindepth。 COM /一切任您需要到专门关于变更检测功能于角8006c51d206f

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

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