After doing some thorough research online, I've identified the root of my issue: multiple instances of a particular service are being created. I need assistance in pinpointing and rectifying this problem within my code.
The secondary service is dependent on the primary service, which is causing duplication.
export class SecondaryService {
constructor(private primarySvc: IPrimaryService){
this.primarySvc.someSubject.subscribe(() => {});
}
}
This is the Primary Service that is encountering duplication:
export class PrimaryService {
someSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor(){}
}
Here is the provider for the Primary Service:
@Injectable()
export class PrimaryServiceProvider extends PrimaryService {
constructor(){
super();
}
}
And here is the provider for the Secondary Service:
@Injectable()
export class SecondaryServiceProvider extends SecondaryService {
constructor(private PrimaryProvider: PrimaryServiceProvider){
super(PrimaryProvider);
}
}
In my app.module.ts file:
@NgModule({
declaration: [SecondaryComponent],
exports: [SecondaryComponent],
imports: [BrowserModule],
providers: [SecondaryServiceProvider, PrimaryServiceProvider ]
})
export class SearchModule{}
When attempting to utilize the component in a local environment, the setup resembles the following:
In app.module.ts
@NgModule({
declaration: [AppComponent, HomeComponent],
imports: [SearchModule, BrowserModule],
providers: [PrimaryServiceProvider, SecondaryServiceProvider],
bootstrap: [AppComponent]
})
export class AppModule{}
Inside home.component.ts:
export class HomeComponent {
constructor( primarySvc: PrimaryServiceProvider,
secondarySvc: SecondaryServiceProvider) {
this.primarySvc.someSubject.next(false);
}
}
It has become evident that there are two instances of the Primary Service due to 'someSubject' being out of sync, with no values fetching from home.component.ts through the subscribe function in the Secondary Service.
I eagerly await guidance on where the necessary modifications should be made. Thank you!