title: How to make components in Angular 2 singleton?
body: <p>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.<br>
  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.  </p>
  
  <h3>As an example, check the following scenario:</h3>
  
  <ul>
  <li>I logged into my application and made a REST call (on window resize event) in a specific component</li>
  <li>An important point here is every user has an unique ID which is used in REST calls</li>
  <li>Then I logged out from this user and logged in with another user</li>
  <li>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</li>
  <li>To check my suspicion, I  created a <em>first class</em> variable in the <em>constructor</em> which basically stores the value of <code>Date.now()</code>. This is turn would tell me when the class was instantiated. </li>
  <li>Then I added a few <code>console.log()</code> statements which would show me which instance was being called by the value of my variable. </li>
  <li>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. 
  <a href="https://i.stack.imgur.com/alKOU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/alKOU.png" alt="This is the image of my log statement. I have blackened out the sensitive parts."></a></li>
  </ul>
  
  <p>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.</p>
  
  <h3>My Questions are:</h3>
  
  <ol>
  <li>Is there a way to make the component class singleton?</li>
  <li>Is there any way to destroy the component instance on leaving the component?</li>
  </ol>