简体   繁体   English

Angular:将组件传递给组件

[英]Angular: Pass Component to a Component

I have this small gridComponent:我有这个小gridComponent:

@Component({
    selector: 'moving-grid',
    templateUrl: './grid.component.html',
    styleUrls: ['./grid.component.css']
})
  export class GridComponent {
     @Input('widgets') extComponents: Array<Component>;
  }

And a second testComponent和第二个 testComponent

@Component({
  selector: 'test',
  template: `
    <div #content>I say hello<li>i</li><li>u</li><button (click)="test()">click me</button> world</div>
  `
})

export class TestComponent {
  @ViewChild('content') content: HTMLElement;

  getContent() {
    return this.content;
  }
  test() {
    console.log("test");
  }
}

Now I'm trying to pass multiple instances of testComponent to the gridComponent.现在我试图将 testComponent 的多个实例传递给 gridComponent。 Therefore I have a third Component which looks like this one:因此,我有第三个组件,如下所示:

template: `
        <moving-grid [widgets]="[z1,z2]"> 
          <test  class="grid-item-content-001" #z1></test>
          <test  class="grid-item-content-002" #z2></test>
        </moving-grid>
      `

Until this point, everything works like expected.到目前为止,一切都按预期进行。 But how can I render the Components from @Input in the gridComponent?但是如何从 gridComponent 中的 @Input 渲染组件? My first approach was to declare a @ViewChild in the testComponent and return it with a getContent()-function.我的第一种方法是在 testComponent 中声明一个 @ViewChild 并使用 getContent() 函数返回它。 But it won't work.但它不会工作。 Can I use the ng-content directive in some way or is there a better solution?我可以以某种方式使用 ng-content 指令还是有更好的解决方案?

The GridComponent looks like this. GridComponent 看起来像这样。 I want to display the templates of a @Input-Component inside one of the black boxes.我想在其中一个黑框内显示 @Input-Component 的模板。 Is it possible?是否可以?

移动网格组件

Thanks for any kind of help感谢您的任何帮助

You should not use an @Input to pass in the components.您不应该使用@Input来传递组件。 You can use @ContentChildren for that and an abstract WidgetComponent :您可以使用@ContentChildren和一个抽象的WidgetComponent

@Component({
    selector: 'moving-grid',
    template: `
      <div class="widget-wrapper">
         <ng-container *ngFor="let widget of widgets">
             <!-- use a ngSwitchCase here for different types-->
             <grid-test-widget [widget]="widget" *ngIf="widget.active && widget.type === 'test'"></grid-widget>
         </ng-container>          
      </div>
    `,
    styleUrls: ['./grid.component.css']
})
export class GridComponent implements AfterContentInit {
     @ContentChildren('widget')
     widgets: QueryList<WidgetComponent>;

     ngAfterContentInit() {
        //your components will be available here
     }
}

The template of your third component will stay the same, but without the [widgets] and an added #widget name:第三个组件的模板将保持不变,但没有[widgets]和添加的#widget名称:

<moving-grid> 
  <test-widget class="grid-item-content-001" #widget [active]="false"></test>
  <test-widget class="grid-item-content-002" #widget></test>
</moving-grid>

Your TestWidgetComponent which will extend an abstract WidgetComponent :您的TestWidgetComponent将扩展一个抽象的 WidgetComponent :

@Component({
  selector: 'test-widget',
  // ...
})
export class TestWidgetComponent extends WidgetComponent {
    public type: string = 'test';
}

And your WidgetComponent :还有你的WidgetComponent

@Directive()
export abstract class WidgetComponent {

   @Input()
   public active: boolean;

   public type: string;

}

And you'll have several grid widgets based on the type of the widget:您将拥有多个基于小部件类型的网格小部件:

@Component({
  selector: 'grid-test-widget',
  template: `<div #content>I say hello<li>i</li><li>u</li><button (click)="test()">click me</button> world</div>`
})
export class GridTestWidgetComponent{}
    

I think I have a very similar use case.我想我有一个非常相似的用例。 The top answer has mostly solved the question, however, if you want to decide dynamically on which exact component you want to render (or rather pass to your grid component), this might be helpful:最佳答案基本上解决了这个问题,但是,如果您想动态决定要呈现的确切组件(或者传递给您的网格组件),这可能会有所帮助:

https://angular.io/guide/dynamic-component-loader https://angular.io/guide/dynamic-component-loader

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

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