2016-09-11 4 views
1

一般的な状況のように見えますが、親ルートから子ルートまでのデータをどのように共有するのか、まったく解決できませんでした。子ルートへのデータの共有

ルート:

{ 
    path: ':id', 
    component: DetailComponent, 
    children: [ 
     { path: 'overview', component: OverviewComponent }, 
     { path: 'profile', component: ProfileComponent } 
    ] 
} 

サービス:

@Injectable() 
export class Service { 
    constructor(private http: Http) { } 

    getModel(id: number): Observable<Profile> { 
     // ajax request to the server using Http service 
     // returns Profile model 
    } 
} 

DetailComponent(親):

@Component({ 
    moduleId: module.id, 
    templateUrl: 'detail.component.html' 
}) 
export class DetailComponent implements OnInit, OnDestroy { 
    model: any = {}; // how do I share this with children components avoiding another ajax request? 

    private paramSub: any; 
    private modelId: number; 

    constructor(
     private activeRoute: ActivatedRoute, 
     private service: Service) { } 

    ngOnInit(): void { 
     this.paramSub = this.activeRoute.params.subscribe(params => { 
      this.modelId = +params['id']; 
      this.service.getModel(this.modelId).subscribe(model => this.model = model); 
     }); 
    } 

    ngOnDestroy(): void { 
     this.paramSub.unsubscribe(); 
    } 
} 

OverviewComponent(子):

@Component({ 
    moduleId: module.id, 
    templateUrl: 'overview.component.html' 
}) 
export class OverviewComponent implements OnInit, OnDestroy { 
    model: any = {}; 

    private paramSub: any; 
    private modelId: number; 

    constructor(
     private activeRoute: ActivatedRoute, 
     private service: Service) { } 

    ngOnInit(): void { 
     this.paramSub = this.activeRoute.parent.params.subscribe(params => { 
      this.modelId = +params['id']; 
      // another request to the server which I need to prevent 
      this.service.getModel(this.modelId).subscribe(model => this.model = model); 
     }); 
    } 

    ngOnDestroy(): void { 
     this.paramSub.unsubscribe(); 
    } 
} 

あなたは親コンポーネントは、子どもたちのルートに渡す必要が共通のデータを受け取り、見ることができるように。可能であれば、このデータをサービスにキャッシュすることができれば、これを達成してキャッシュを無効にする正しい方法がありますか?このすべてが良い練習でない場合は、適切な方法を提案してください。

angular:2.0.0-rc.6;ルータ:3.0.0-rc.2

+0

通常の共有サービスは、このような状況https://angular.io/docs/ts/latest/cookbook/component-communication.html#ための最良の方法です! #bidirectional-service –

+0

[corner2 HTTPサービスでのキャッシング結果]の重複の可能性があります(http://stackoverflow.com/questions/34104277/caching-results-with-angular2-http-service) – tibbus

+0

これは、 http://www.astackoverflow.com/questions/34104277/caching-results-with-angular2-http-service – tibbus

答えて

0
import { Observable } from 'rxjs/Observable'; 
... 

@Injectable() 
export class Service { 
    private model: Profile; 
    constructor(private http: Http) { } 

    getModel(id: number): Observable<Profile> { 
     if (this.model) { 
      return Observable.of(this.model); 
     } 

     // ajax request to the server using Http service 
     // returns Profile model 
     return http.get(...).map(res => this.model = res) 
    } 
} 
関連する問題