I am trying to make a global object for some part of the data from one service, that service is observable. So other components can use it.
I found a way call subject to handle this state management. I tried, and it's like half way working.
entity.ts file
export class DataDe {
name:string;
age:number;
constructor(){
this.name = "";
this.age = 0;
}
}
service file
@Injectable({
providedIn: 'root'
})
export class DeService {
sub= new BehaviorSubject<DataDe>(null);
dataStire: DataDe = new DataDe();
constructor(private http: HttpClient){}
getData(id:any):Observable<any>{
this.http.get(Url.getDetails+"id="+id).pipe(first()).subscribe(res=>{
this.dataStire.name = res[0]['name'];
this.dataStire.age = res[0]['age'];
}
this.returnSubject.next(Object.assign({},this.dataStire));
return this.http.get(Url.getDetails+"id="+id)
}
}
other component ts file
export class People implements OnInit{
constructor(public de:DeService){}
ngOnInIt(){
console.log(this.de.dataStire); //
console.log(this.de.dataStire.age); //I want to get 4, but I only got 0
}
}
the actual result I got is an object looks like enter image description here