How to get a new instance / restart all services when logging out in Angular2

I am developing an angular2 application that is similar to the Plunkr link provided at the end.

In HeroService, I have a clickCount property that will increase when the hero clicks. But after logging in, I want to reset this parameter. I do not want to use the service and reset its value (in fact, in my application I use many services, so I can not use every service and reset its value when I exit the system).

Is there any method to reset / reload a service for a specific action without reloading the entire application. Hero Service Below

import { Hero } from './hero'; import { HEROES } from './mock-heroes'; import { Injectable } from '@angular/core'; @Injectable() export class HeroService { getHeroes(): Promise<Hero[]> { return Promise.resolve(HEROES); } getHeroesSlowly(): Promise<Hero[]> { return new Promise<Hero[]>(resolve => setTimeout(resolve, 2000)) // delay 2 seconds .then(() => this.getHeroes()); } getHero(id: number): Promise<Hero> { return this.getHeroes() .then(heroes => heroes.find(hero => hero.id === id)); } } 

Plunkr link

+1
source share
1 answer

I don’t think there is a way other than destroying and reloading the entire Angular2 application.

I would just use a globally shared service with the observables of all other services that signed up. The shared service throws an event to notify the others that they should reset, and these services will then do it themselves.

+2
source

All Articles