0

"@angular/common": "^5.0.0"

I searched a lot about it and most say to do it that way ... What am I doing wrong?

My intuition says that every time that I build (constructor) another component, the GlobalService will be clean, but I don't know how to do this correctly...

GlobalService

import { Injectable } from '@angular/core';

@Injectable()
export class GlobalService{
  private currency;
  constructor() {
   }
  setCurrency(val) {
      this.currency = val;
  }
  getCurrency() {
      return this.currency;
  }
}

GlobalService Module

@NgModule({
  ...,
  providers:[ GlobalService ]
})

Setting Value (Any Component)

import { GlobalService } from '.path/global.service';

constructor(..., private global: GlobalService) { }

anyMethod(){
  this.global.setCurrency('Test Global Service');
  console.log(this.global.getCurrency()); // On console => Test Global Service
}

Getting Value (Another Any Component)

import { GlobalService } from '.path/global.service';

constructor(..., private global: GlobalService) { }

anyMethod(){
  console.log(this.global.getCurrency()); // On console => undefined
}
LeoHenrique
  • 229
  • 5
  • 16
  • Is Another Any Component rendering before Any Component? – SiddAjmera Jul 30 '19 at 13:17
  • @SiddAjmera, No, after – LeoHenrique Jul 30 '19 at 13:19
  • 1
    Have you added `providers:[ GlobalService ]` to the `@Constructor` of any of these two components? If not, is one of your Modules lazily loaded? If not, please consider replicating this issue on a Sample StackBlitz and share it across so that it could be looked into. – SiddAjmera Jul 30 '19 at 13:20
  • Thinking like: LoginView (AnyComponent) set the email on GlobalServie and I can use that email in all of other components... I currently use localStorage to do this and I want to use a Global now – LeoHenrique Jul 30 '19 at 13:22
  • Ok, I'll create a StackBlitz – LeoHenrique Jul 30 '19 at 13:22
  • Simplest way to check if there actually are multiple service instances created: just add some console output to service CTOR. Most likely would still be that one component calls getCurrency() before another has set a value. – Ronald Korze Jul 30 '19 at 13:28
  • I create a StackBlitz (https://stackblitz.com/edit/angular-qrvaxy) and worked like I want .... but on my project (above) don't work – LeoHenrique Jul 30 '19 at 14:02
  • @RonaldKorze, I did what you told me to do and it's creating multiple instances... So, how do I create a singleton? I searched for this too, but the most is for Angular 6 – LeoHenrique Jul 30 '19 at 14:09
  • Where is your GlobalServiceModule imported? You need to make sure it's only imported once and that either in the app module or if you have a core module. – Erbsenkoenig Jul 30 '19 at 14:32
  • @Erbsenkoenig, only on `app.module.ts` – LeoHenrique Jul 30 '19 at 14:34
  • Any one have any idea about what can I try to solve this? Or some reference for me to study more – LeoHenrique Jul 30 '19 at 16:16
  • Solved with this: https://stackoverflow.com/a/47213845/10647645 – LeoHenrique Jul 30 '19 at 18:46

0 Answers0