Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make components in Angular 2 singleton?

I am using Angular 2 to build my web application which which has lot of components. I am using angular-cli to create, run and build the project.
Accidentally (or luckily) I stumbled across a bug where I realized that multiple instances of my component were being created. Even worse was when I realized that my code was referring to any one this instances randomly without any logic to trace it back.

As an example, check the following scenario:

  • I logged into my application and made a REST call (on window resize event) in a specific component
  • An important point here is every user has an unique ID which is used in REST calls
  • Then I logged out from this user and logged in with another user
  • I went back to same component and made the same rest calls (again on window resize events), however to my shock, some of the rest calls were being made using the unique ID or earlier logged in user
  • To check my suspicion, I created a first class variable in the constructor which basically stores the value of Date.now(). This is turn would tell me when the class was instantiated.
  • Then I added a few console.log() statements which would show me which instance was being called by the value of my variable.
  • The log confirmed my suspicion that multiple instances indeed exist simultaneously and there is no certain logic or path followed to access anyone of them. This is the image of my log statement. I have blackened out the sensitive parts.

This is the image of my log statement. I have blackened out the sensitive parts. It can be clearly seen that some rest calls are being made with the unique id for tenant 1 while some for trial tenant. Also use of two instances is also very clear from two instance times. The old instance of previous logged in tenant is somehow still in play and my component is still able to access it.

My Questions are:

  1. Is there a way to make the component class singleton?
  2. Is there any way to destroy the component instance on leaving the component?
like image 982
theHeman Avatar asked Jan 31 '17 10:01

theHeman


People also ask

Are components singleton in Angular?

The answer would be no. The main objective of angular services is to share data across Angular application. Practically an angular service can be shared between all the components or can be limited to some component. Hence Angular service can be a singleton as well as non-singleton in nature.

What is singleton pattern in Angular?

A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object.

Can we create multiple instances of service in Angular?

Angular will create new instances for any of InjectionToken or Injectable in cases of using: lazy loaded modules. child injector modules.

Can we create component inside component in Angular?

What are Angular Nested Components? The Angular framework allows us to use a component within another component and when we do so then it is called Angular Nested Components. The outside component is called the parent component and the inner component is called the child component.


1 Answers

  1. Is there a way to make the component class singleton?

not that i am aware of

  1. Is there any way to destroy the component instance on leaving the component?

yes, there is an interface OnDestroy

export class ClockComponent implements OnDestroy {
interval;

ngOnDestroy() {
  clearInterval(this.interval);
}

constructor() {
  this.interval = setInterval( ()=> console.log('tick') );
}
like image 190
bresleveloper Avatar answered Oct 14 '22 10:10

bresleveloper