instantiation - angular 4 - unwanted multiple service instances -


this might tricky question , i'm hoping provide enough information solve since i'm trying hours.

i'm pretty sure have somehow instantiated 2 services since changes initiated 1 components, didn't affect other.

below module structure.

sharedmodule //reuseable cmps , on..  appmodule({   imports:[sharedmodule, pagemodule] })  pagemodule({   imports:[sharedmodule,calendarmodule] })  calendarmodule({   imports:[sharedmodule],   declarations:[caldendarmonthcmp, calendarcmp, calendardaycmp]   providers:[calendarservice] }) 

the calendarservice has initialise method passes initial data.

calendardays$: behaviorsubject<sbcal.sbcalendarday[]> = new behaviorsubject([]);  intializedays(day: sbcal.sbcalendarday) {   let newdays = [...data] // calculations, array not empty   this.calendardays$.next(newdays); } 

caldendarmonthcmp displays calendar in month grid , when click day, initializes , passes on newdays arrays , opens calendardaycmp in new page.

calendardaycmp subscribes calendardays$ gets empty array , not newdays.

my suspicion 2 service instances hardened when changed code to

calendardays$: behaviorsubject<sbcal.sbcalendarday[]>; intializedays(day: sbcal.sbcalendarday) {   let newdays = [...data] // calculations   this.calendardays$ = new behaviorsubject(newdays); } 

instantiating subject if initialize has been called. in case opening calendardaycmp throws , can't subscribe undefined error.

i've read through angular ngmodule description , far understand, instantiating more services happens if provide service other modules?

thanks help!


Comments