Angular 2 Sibling Component Communication

前端 未结 12 1538
深忆病人
深忆病人 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:22

    This is not what you exactly want but for sure will help you out

    I'm surprised there's not more information out there about component communication <=> consider this tutorial by angualr2

    For sibling components communication, I'd suggest to go with sharedService. There are also other options available though.

    import {Component,bind} from 'angular2/core';
    import {bootstrap} from 'angular2/platform/browser';
    import {HTTP_PROVIDERS} from 'angular2/http';
    import {NameService} from 'src/nameService';
    
    
    import {TheContent} from 'src/content';
    import {Navbar} from 'src/nav';
    
    
    @Component({
      selector: 'app',
      directives: [TheContent,Navbar],
      providers: [NameService],
      template: ''
    })
    
    
    export class App {
      constructor() {
        console.log('App started');
      }
    }
    
    bootstrap(App,[]);
    

    Please refer to link at top for more code.

    Edit: This is a very small demo. You have already mention that you have already tried with sharedService. So please consider this tutorial by angualr2 for more information.

提交回复
热议问题