简体   繁体   English

如何将 TypeScript 接口传递给输入?

[英]How can I pass TypeScript interfaces to an Input?

If I have the following interface:如果我有以下界面:

/** carddetail.ts */
export interface CardDetail {
   name: string;
   type: string;
   logo?: string;

How can I make sure @Input() decorators accept this type of data?我怎样才能确保@Input()装饰器接受这种类型的数据?

import { CardDetail } from '../card-detail'

@Input cardDetail: CardDetail
export class CardComponent {
}

This gives me the error: 'CardDetail' only refers to a type, but is being used as a value here.ts(2693)这给了我错误: 'CardDetail' only refers to a type, but is being used as a value here.ts(2693)

Is there some other approach to use?还有其他方法可以使用吗? In my parent class I define a在我的父母 class 中,我定义了一个

carddetails = CardDetail[]

constructor() {
 this.carddetails = [
      { name: "foo", logoUrl:'/assets/foo.png', type: "bar" } 
      ...
 ]

and pass those values in an *ngFor in the template并将这些值传递到模板中的 *ngFor 中

<ng-template *ngFor="let cardDetail of carddetails">
    <app-card [cardDetail]="cardDetail"></app-card>
</ng-template>

@Input should be inside component class.Also you will need to add () after @Input so it should be @Input应该在组件 class 内。您还需要在@Input之后添加()所以它应该是

export class CardComponent {
   @Input() cardDetail: CardDetail
}

instead of代替

@Input cardDetail: CardDetail
export class CardComponent {
}

also from your parent class it seems like you are passing an array if so,then it should become同样来自你的父母 class 如果是这样的话,你似乎正在传递一个数组,那么它应该变成

@Input() cardDetail: CardDetail[];

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

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