1

I have 6 methods, where they're called several times around the app:

Here are few of them:

getLegal(){
    let status = 'Active'; 
    this.auth.getLegalIndividual(status).subscribe(

      (data)=>{
        this.legalData = data;
      },
      (error)=>{
        console.log(error);
      }
  )

}

getSituation(){
    let status = 'Active';
    this.auth.getSituationIndividual(status).subscribe(
      (data)=>{
        this.situationData = data;
      },
      (error)=>{
        console.log(error);
      }
    )
  }

Each time they're called the data is saved in an object and used.

I want to run them only one single time at the load of the app in app.component.ts and save the returned results into global objects and it will accessed by components where it should be used.

ngOnInit(){
    this.getLegal();
    this.getSituation();
  }

Auth Script:

getSituation(status) {
    let httpParam = new HttpParams().set('type', 'unit')
                                    .set('status', status);
    return this.http.post(this.globalVar.GetSituationUrl, httpParam);
  }

Where this.globalVar.GetSituationUrl is the url to the php script.

Is method going to slow down the load of the app ? And on changing routes, the ngOnInit() going to run again?

I am slightly new to angular and few things are obscure.

alim1990
  • 4,656
  • 12
  • 67
  • 130
  • 1
    It is not going to slow down your app, they are async methods. I presume, you are calling a restful API in `getLegal()` and `getSituation()`, so it's fine they will be subscribed only once (coz they emit only once) – Ashish Ranjan Dec 13 '18 at 08:15
  • @xyz let me show you the `auth` service for these methods to be more sure. – alim1990 Dec 13 '18 at 08:16
  • @xyz check the auth script please. – alim1990 Dec 13 '18 at 08:17
  • 1
    Yeah, they look fine to me. Why do you think that it's slowing down your app? (Although I believe it's not required, you can have 2 class variables and assign them to subscriptions, unsubscribe to them in `ngOnDestroy()`. SOmething like: `this.sub1 = this.auth.getLegalIndividual....` and in `ngOnDestroy -> if (this.sub1) { this.sub1.unsubscribe() }`) – Ashish Ranjan Dec 13 '18 at 08:21
  • Thanks @xyz. Really appreciate it. – alim1990 Dec 13 '18 at 08:28
  • @xyz if the admin updated data related to these methods, it will not appear on users unless they reload the app. Do you recommend using socket.io with angular and php to detect the change and update the data ? – alim1990 Dec 13 '18 at 08:38
  • It completely depends on the requirement of your application. If you need live data everytime, sockets will serve you best. If you don't need continuous live updates, then you can run those methods periodically.(Rememberr to unsubscribe in both the cases) – Ashish Ranjan Dec 13 '18 at 08:46
  • Does socket.io works with php and angular 7 ? @xyz – alim1990 Dec 13 '18 at 09:44
  • I haven't used myself, see this: https://stackoverflow.com/questions/6398887/using-php-with-socket-io – Ashish Ranjan Dec 13 '18 at 12:21

0 Answers0