Angular 两个组件之间的值共享2

Angular 两个组件之间的值共享2,angular,components,angular2-routing,angular-components,Angular,Components,Angular2 Routing,Angular Components,我正在使用开放层贴图开发angular 2应用程序。因为我需要共享从一个组件到其他组件的位置坐标 一个组件 placesChanged() { var places = this.searchBox.getPlaces(); if (places == null) return; if (places == undefined) return; if (places.length == 0) return; this.searchResultsSourc

我正在使用开放层贴图开发angular 2应用程序。因为我需要共享从一个组件到其他组件的位置坐标

一个组件

placesChanged() {
    var places = this.searchBox.getPlaces();
    if (places == null) return;
    if (places == undefined) return;
    if (places.length == 0) return;

    this.searchResultsSource.clear();

    this.bounds = new google.maps.LatLngBounds();
    // For each place, get the icon, name and location.
    places.forEach(this.processPlace.bind(this));

    // if the drawn result(s) is only one
    if (this.searchResultsSource.getFeatures().length == 1) {
        var singleFeature = this.searchResultsSource.getFeatures()[0];
        this.pos = singleFeature.getGeometry().getCoordinates();
        console.log(this.pos);
    }
    this.animateView(places.length);

}
constructor(public dialogRef: MdDialogRef<BufferDialogComponent>,public searchcomponent: SearchComponent) {console.log(this.searchcomponent.pos);}
在这里,我将pos值作为地理坐标 我想把这个pos值分享给其他组件

其他组成部分

placesChanged() {
    var places = this.searchBox.getPlaces();
    if (places == null) return;
    if (places == undefined) return;
    if (places.length == 0) return;

    this.searchResultsSource.clear();

    this.bounds = new google.maps.LatLngBounds();
    // For each place, get the icon, name and location.
    places.forEach(this.processPlace.bind(this));

    // if the drawn result(s) is only one
    if (this.searchResultsSource.getFeatures().length == 1) {
        var singleFeature = this.searchResultsSource.getFeatures()[0];
        this.pos = singleFeature.getGeometry().getCoordinates();
        console.log(this.pos);
    }
    this.animateView(places.length);

}
constructor(public dialogRef: MdDialogRef<BufferDialogComponent>,public searchcomponent: SearchComponent) {console.log(this.searchcomponent.pos);}
构造函数(public dialogRef:MdDialogRef,public searchcomponent:searchcomponent){console.log(this.searchcomponent.pos);}
在pos上,我得到未定义的错误。
请帮帮我。

这两个组件之间有父子关系吗?@TheDictator No!!若您在构造函数中进行控制台操作,显然它会显示未定义,因为那个是在您的服务具有那个值之前执行的。将控制台移到其他功能并检查。@AmitChigadani我试过了,但仍然显示未定义。。!