简体   繁体   English

在我们在Angular 7中重用它们时如何将参数传递给其他组件

[英]How to pass arguments to other components while we reusing them in Angular 7

I have three components namely, Dashboard,Widget and Workspace Components. 我有三个组件,即仪表板,小组件和工作区组件。 I have reused widgets components in the other two. 我在其他两个中重用了小部件组件。 I have to display widgets component with orginal data from DB while reusing it from Dashboard Component, and I need to display dummy data while reusing it from Workspace Component. 我必须使用来自DB的原始数据显示窗口小部件组件,同时从Dashboard Component重用它,我需要在从Workspace Component重用它时显示虚拟数据。 To achieve the above scenario I have assigned an boolean variable named dummyData and set it to false by default in my WidgetComponent. 为了实现上述场景,我已经分配了一个名为dummyData的布尔变量,默认情况下在我的WidgetComponent中将其设置为false。

public dummyData : boolean = false;

ngOnInit(){
  if(!this.dummyData){
    // orginal data
  }else{
    // dummy data
  }

Now, I need to pass an boolean argument while reusing this component somewhere else. 现在,我需要传递一个布尔参数,同时在其他地方重用该组件。

How do I pass the argument to the component while reusing it? 如何在重用时将参数传递给组件?

you can use Input() directive in your Widget component and pass the state into the component where ever you want to use it. 您可以在Widget组件中使用Input()指令,并将状态传递到您想要使用它的组件中。

  • Widget component 小部件组件

@Input() stateData: boolean; @Input()stateData:boolean;

  • In your selector declaration 在您的选择器声明中

refer the component interaction part https://angular.io/guide/component-interaction 参考组件交互部分https://angular.io/guide/component-interaction

Use the input decorator on the property. 在属性上使用输入装饰器。

@Input()
dummyData = false;

and set it true with 并设置为true

<widget [dummyData]="true"></widget>

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

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