我正在使用开放层图制作角度2应用程序。因为我需要使用共享服务将一个组件的位置坐标共享到其他组件。
searchcomponents.ts
bounds: any;
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 ,"hanumanh" );
this.service.setUrlHistoryObj(this.pos);
}
我想将pos值分享给其他组件。 这是我的服务代码
Search.services.ts
import {Injectable} from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SearchService {
public pos : string;
public invokeEvent:Subject<any> = new Subject<string>();
constructor() {
this.pos="";
}
public setUrlHistoryObj(val: string): void {
this.pos = val;
console.log(val);
}
public getUrlHistoryObj(): string {
return this.pos;
}
在这里我得到pos值作为geocoordinate,我想将pos值分享给其他组件。
Buffer.components.ts
value1: string;
constructor(
private urlHistoryService:SearchService) {
this.value1 = this.urlHistoryService.getUrlHistoryObj();
// this.value1 = this.urlHistoryService.setUrlHistoryObj();
}
我的错误未定义。请帮帮我。