简体   繁体   中英

communication between two child components

I have two child components. One is a filter and another one is a table. When I click in a button which is in filter component I want to call onInit() from table component (or do something better for refresh the table). How can I do it?

something like this:

//function from filter component
filter() { ... }

//function from table component
populateTable(){  .... }


//I want to do something like
filter(){ this.populateTable(); }

How can i do it

Organize as a heirarchy

- Parent
  - Filter 
  - Table 

The state will be in Parent . A user interaction in Filter only requests Parent to change the state causing a re-render of the Table .

Solution1:

@ViewChild('tablecmp') tablecmp: TableComponent;
@Component({
  selector: 'filtercomp',
  .....
}
export class FilterComponent{
   filter(){
     this.tablecmp.table();
   }
}

Filter Component Template:

<tablecomp #tablecmp></tablecomp>

Table Component

@Component({
  selector: 'tablecomp',
  .....
}
export class TableComponent{
   table(){

   }
}

Solution2:

Instead of calling the child component function (table()) from parent component (filter(). You can apply filter for the data in the parent component and send the result to child component using @input() to display.

We prefer Solution2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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