简体   繁体   English

角度组件的类型

[英]Type of angular components

Can I refer to angular component class as a type?我可以将角度组件类称为一种类型吗? I see Ionic uses any for components.我看到 Ionic 使用any作为组件。 Is it possible in Typescript to declare an argument type, that expects only component classes?是否可以在 Typescript 中声明一个只需要组件类的参数类型?

I saw this question, but components don't have anything in common in constructors: Passing class as parameter causes "is not newable" error我看到了这个问题,但是组件在构造函数中没有任何共同点: 将类作为参数传递会导致“不可更新”错误

There is ComponentType<T> .ComponentType<T> Maybe it will help.也许会有所帮助。

import { ComponentType } from '@angular/cdk/overlay'; 

As it turns out this is not possible in TypeScript right now.事实证明,目前这在 TypeScript 中是不可能的。 Decorators cannot change type of a class, that means they are invisible for a type system.装饰器不能改变类的类型,这意味着它们对于类型系统是不可见的。
More info can be found here:更多信息可以在这里找到:

If they are components that are created dynamically, Angular provides the ComponentRef class.如果它们是动态创建的组件,Angular 会提供 ComponentRef 类。

See here: https://angular.io/api/core/ComponentRef见这里: https ://angular.io/api/core/ComponentRef

A statically declared component won't have this class.静态声明的组件不会有此类。 Although, you can pass around Component identifiers as an untyped variable as needed, as long as you import them into the relevant components / services.虽然,您可以根据需要将组件标识符作为无类型变量传递,只要将它们导入相关组件/服务即可。 If you're using dependency injection, ie, to inject a component instance into its host directive, the 'type' would just be whatever its name is, like so -如果您正在使用依赖注入,即将组件实例注入其主机指令,则“类型”将只是其名称,就像这样 -

 import { Directive } from '@angular/core'
 import { MyNavigatorComponent } from '../components/my-nav.component'

 constructor (private navigator: MyNavigatorComponent) {

From: https://angular.io/api/core/ComponentRef来自: https ://angular.io/api/core/ComponentRef

The Following represents a type that a Component or other object is an instance of.以下表示组件或其他对象是其实例的类型。

An example of a Type is MyCustomComponent class, which in JavaScript is represented by the MyCustomComponent constructor function. Type 的一个示例是 MyCustomComponent 类,它在 JavaScript 中由 MyCustomComponent 构造函数表示。

Code sample:代码示例:

import { Type } from "@angular/core";

export type MyDynamicComponentType = Type<any>;

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

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