내가 작업 코드가 각 4의 상위/하위 구성 요소 간의 서비스 데이터를 공유하려고하지만 최선의 선택 이잖아 경우 확실하지 오전과 부모 아이. 주제를 만들고 관찰 가능한 방법을 구독하여 부모 -> 자식 사이에서 의사 소통하기 위해 주사 가능한 서비스 클래스를 사용하고 있습니다. 이제 역방향으로 의사 소통을하기 위해 : child -> parent, Subject를 만들고 parent에서 observable에 가입하여 동일한 서비스 클래스를 다시 사용하고 있습니다. 부모와 자식 모두 관찰 할 수있는 방식으로 구독하는 방식으로 올바른 접근 방식입니까? 나는 다른 곳에서 사람들이 @Output 데코레이터를 제안하여 child -> parent 사이에서 통신하는 것을 보았지만 나의 코드는 subscribe 메커니즘으로 작업하고있다. 앞으로 메모리 누수와 같은 문제가 발생할 것인가?각도 4 구성 요소 통신 - 가입자
부모 구성 요소
constructor(private _textdataservice: TinyEditorService, private _gmapService: GmapService) {
this.subscription = this._gmapService.getMessageC2P().subscribe((message) => {
this.message = message;
this.childCallingParent(message);
});
this.subscription = this._gmapService.getStoreSearchRequest().subscribe((radius) => {
this.radius = radius;
this.retrieveNearByLocations(radius);
});
}
하위 구성 요소 ->
constructor(private _gmapService: GmapService) {
// subscribe to home component messages
this.mainSubscription = this._gmapService.getMessageP2C().subscribe((addresses) => {
this.mainCoordinates = addresses;
});
this.storeSubscription = this._gmapService.getMessageP2CStore().subscribe((addresses) => {
this.storeCoordinates = addresses;
if(this.storeCoordinates){
for(let coord of this.storeCoordinates){
this.addNearbyStoremarker(coord.name, coord.latitude, coord.longitude);
}
}
});
}
서비스 ->
export class GmapService {
private _dataurl='/assets/gmapmarkers.json';
constructor(private _http: Http){}
private parentSubject = new Subject<IGmapData[]>();
private storeSubject = new Subject<IGmapData[]>();
private childSubject = new Subject<String>();
private radiusSubject = new Subject<number>();
sendMessageP2C(latLngArray: IGmapData[]) {
this.parentSubject.next(latLngArray);
}
sendMessageP2CStore(latLngArray: IGmapData[]) {
this.storeSubject.next(latLngArray);
}
sendMessageC2P(message: string) {
this.childSubject.next(message);
}
requestNearByLocations(radius: number) {
this.radiusSubject.next(radius);
}
clearMessage() {
this.parentSubject.next();
this.childSubject.next();
}
getMessageP2C(): Observable<IGmapData[]> {
return this.parentSubject.asObservable();
}
getMessageP2CStore(): Observable<IGmapData[]> {
return this.storeSubject.asObservable();
}
getMessageC2P(): Observable<string> {
return this.childSubject.asObservable();
}
getStoreSearchRequest(): Observable<number> {
return this.radiusSubject.asObservable();
}
getStoreMarkers(): Observable<IGmapData[]> {
return this._http.get(this._dataurl)
.map((response: Response) => <IGmapData[]> response.json());
}
}
thx. – whizKid
여러분을 환영합니다! – Mehdi