Angular 2 Sibling Component Communication

前端 未结 12 1563
深忆病人
深忆病人 2020-11-22 06:31

I have a ListComponent. When an item is clicked in ListComponent, the details of that item should be shown in DetailComponent. Both are on the screen at the same time, so

12条回答
  •  面向向阳花
    2020-11-22 07:04

    One way to do this is using a shared service.

    However I find the following solution much simpler, it allows to share data between 2 siblings.(I tested this only on Angular 5)

    In you parent component template:

    
    
    
     
    

    app-sibling2.component.ts

    import { AppSibling1Component } from '../app-sibling1/app-sibling1.component';
    ...
    
    export class AppSibling2Component {
       ...
       @Input() data: AppSibling1Component;
       ...
    }
    

提交回复
热议问题