如何将数据从父组件绑定到子组件?

huangapple go评论139阅读模式
英文:

How Can Ibinding data from parent component to child component?

问题

我有一个父组件有许多属性,每次都需要使用一些子组件,首先,当我需要主组件时,我会使变量dashboardState == 'main'。

<div *ngIf="dashboardState === 'main'" class="col-12 p-0 p-md-1 h-100">
   <app-main-dashboard [site]="site" [siteContent]="siteContent"></app-main-dashboard>
</div>

但我知道这不是一个好主意,因为现在我有很多子组件,所以我寻找更好的方法,我发现我可以使用router-outletchildren一起工作,这是一种好方法之一,但我发现了一个问题,如何共享父组件上的属性,我唯一的办法是创建一个服务,然后使用这个服务共享所有我的数据。或者,每次需要子组件时,我可以调用它们,例如,如果我需要组件X,我会调用组件X并销毁其他组件,就像这样。

@ViewChild('container', { read: container }) container: ViewContainerRef;

onBuildChildComponent() {
  const componentRef = this.container.createComponent(componentChild);
  componentRef.instance.param01 = param01;
  componentRef.instance.param02 = param02;
  componentRef.changeDetectorRef.detectChanges();
}

什么是处理许多子组件并且需要与它们共享许多属性的最佳方法?当然,如果您有其他建议,请不要犹豫。

英文:

I have a parent component has many properties, and I need every time to use some component child, firstly I worked with conditions when I need the main component I make the variable dashboardState == 'main'.

&lt;div *ngIf=&quot;dashboardState === &#39;main&#39;&quot; class=&quot;col-12 p-0 p-md-1 h-100&quot;&gt;
   &lt;app-main-dahsboard [site]=&quot;site&quot; [siteContent]=&quot;siteContent&quot;&gt;&lt;/app-main-dahsboard&gt;
&lt;/div&gt;

but I know that is not a good idea because now I have more a lot of component child, so I search for a better way I find that I can work with router-outlet with children, it's one of the good ways, but I find a problem to share the properties that I have on the parent component, I have only one way is to create a Service and I will share all my data using this service.
or I can invoke child component every time I need them for example if I need component X I will invoke component X with destroying other components. like this way.

@ViewChild(&#39;container&#39;, { read: container }) container: ViewContainerRef;
onBuildChildCompoenent() {
  const componentRef = this.container.createComponent(componentChild);
  componentRef.instance.param01 = param01;
  componentRef.instance.param02 = param02;
  componentRef.changeDetectorRef.detectChanges();
}

what is the best way to work with a lot of child components and also I need to share a lot of properties with them? and of course, if you have another suggestion, please don't hesitate.

答案1

得分: 1

@Input():
父组件模板中,请添加以下内容:

<child [site]="site" [siteContent]="siteContent"></child>

然后在 child-component 中:

@Input() site: any;
@Input() siteContent: any;

为什么您说“现在我有很多子组件”?
您没有使用 *ngFor 或其他方式来添加多个 <child> 标签。
无论如何,即使是这种情况,也没有问题。


使用 router-outlet:

{
    path: '',
    component: HomeComponent,
    children: [
      { path: '', redirectTo: '/site', pathMatch: 'full' },
      {
        path: 'site',
        data: {
            some_data: 'value'
        },
        loadChildren: () => import('./home/site/site.module').then(m => m.SiteModule)
      },
      { path: '**', redirectTo: '/' }
    ]
}

然后在您的 site-component 中:

ngOnInit() {
  this.route.data.subscribe(console.log);
}
英文:

To pass data from parent to children, you have several ways :

@Input() :
In parent component template, put this

&lt;child [site]=&quot;site&quot; [siteContent]=&quot;siteContent&quot;&gt;&lt;/child&gt;

And in child-component :

@Input() site: any;
@Input() siteContent: any;

Why are you saying "now I have more a lot of component child".
You don't have any *ngFor or anything else to have several <child> tag.
Anyway, even if it's the case, that's not a bad thing.


With router-outlet :

{
    path: &#39;&#39;,
    component: HomeComponent,
    children: [
      { path: &#39;&#39;, redirectTo: &#39;/site&#39;, pathMatch: &#39;full&#39; },
      {
        path: &#39;site&#39;,
        data: {
            some_data: &#39;value&#39;
        },
        loadChildren: () =&gt; import(&#39;./home/site/site.module&#39;).then(m =&gt; m.SiteModule)
      },
      { path: &#39;**&#39;, redirectTo: &#39;/&#39; }
    ]
}

Then in your site-component :

ngOnInit() {
  this.route.data.subscribe(console.log);
}

huangapple
  • 本文由 发表于 2020年1月6日 18:51:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定