0

I am little confused. Reading this stackoverflow question.

https://stackoverflow.com/questions/51395830/why-is-constructor-call-each-time-after-injection-of-service

Here for older version of angular they say that constructor from service is called each time it is initialized. I am using angular version 10 and by me is called just once. So my question is if anybody knows why ?

The test code that i have are two components.In their constructor's is injected the testService. And in the constructor from the testService i have just console log statement.

TEST SERVICE

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

@Injectable({
  providedIn: 'root'
})
export class TestService {
  constructor() { console.log('constructor called')};
}

HOME COMPONENT

export class HomeComponent implements OnInit {
  constructor(private testService: TestService) { };
}

LAYOUT COMPONENT

export class LayoutComponent implements OnInit {
  constructor(private testService: TestService) { };
}

APP COMPONENT

<a routerLink='./layout'>go to layout</a>
<a routerLink='./home'>go to home </a>

APP ROUTING MODULE

const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'layout', component: LayoutComponent }
];

APP MODULE

@NgModule({
  declarations: [
    AppComponent,
    LayoutComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

So, my app component is initialized. When i click on - go to home - then it redirects me to the home component.It is initialized and i see - constructor called - in the console. But when i go on layout component after that i don't see the statements in the console.

Why this time the constructor is not initialized ?

And it happens in the opposite, if i go first to layout i see console log statements and after if i go back to app component and then go to home i see nothing.

pece
  • 87
  • 9
  • Please share the code where and how you have registered the service? – Vimal Patel Nov 30 '20 at 08:59
  • I edited the question Vimal. Tnx for the feedback – pece Nov 30 '20 at 09:00
  • I imported in the question also my app.module.ts file and whole test.service file – pece Nov 30 '20 at 09:01
  • 1
    https://www.thirdrocktechkno.com/blog/are-the-angular-services-singleton/ – MoxxiManagarm Nov 30 '20 at 09:04
  • 1
    Because you have mentioned the providedIn is "root" then only single instance of the service will be created. – Vimal Patel Nov 30 '20 at 09:17
  • Okay,which changes should i make if i want to create instance every time ? – pece Nov 30 '20 at 09:25
  • Aren't you better of with just instantiating a normal class? Another solution would be to add a "init()" method and call that everytime you need a "new" instance. Singleton services are meant to persist, and recreating singleton services (while possible) can often be confusing. https://angular.io/guide/singleton-services –  Nov 30 '20 at 09:37
  • If you want to initialize them separately each time you go to home and layout give it in the providers of both home-module and layout-module ( If thats what you really want) – Gokul Prasannan Nov 30 '20 at 09:41

1 Answers1

2

Your service is provided in root.

@Injectable({
  providedIn: 'root'
})

This means that your service will be instantiated only once, it is a singleton. If you want don't want it to be a singleton, you can change the providedIn value to something like this:

@Injectable({
  providedIn: MyModule
})

Or directly define your service in the providers of your module and remove the providedIn:

MY-MODULE

@NgModule({
  imports: [
    ...
  ],
  declarations: [
    ...
  ],
  providers: [
    TestService
  ]
})
export class MyModule { }

TEST-SERVICE

@Injectable()
export class TestService {}
Tombery
  • 266
  • 2
  • 15
  • Hello Tombery.I tried your solution. But again the constructor in test service is called only once ! even the root is removed from Injectable and the service is declared in the providers array – sdsd Jan 18 '21 at 16:42
  • If we declare the service in the providers array in the components itself, then the constructor will be called every time,So your answer is wrong – sdsd Jan 18 '21 at 16:49