Khurshid Ansari
Khurshid Ansari

Reputation: 5095

Angular reinitialize component when i go back to previous component

I am using angular v5.

I have home component and home2 component

routing set up :

{
  path: "home", 
  component: homecomponent
}, 
{
  path:"home2", 
  component: home2component
}
{
  path:"**", 
  redirectTo:"/home", 
  pathMatch: "full"
}

I am facing issue when redirect back to home component. home component complete reinitialise.

How to manage home component's state?

Upvotes: 0

Views: 1164

Answers (2)

Sreeram Nair
Sreeram Nair

Reputation: 2397

Managing state is a hard problem. We need to coordinate multiple backends, web workers, and UI components, all of which update the state concurrently.

You can use Ngrx to manage State of a component in Angular 2+

This is the interaction between those components in NgRx:

This is the interaction between those components in NgRx

There are five parts that constitute NgRx:

  1. Store
  2. List item
  3. Reducers (and Meta-Reducers)
  4. Actions
  5. Selectors
  6. Effects

The Ngrx provides a single source of truth for the state of your application.

You can understand more about Ngrx here

Upvotes: 1

Tony
Tony

Reputation: 20132

That is normal behavior because when you visit your angular component Angular will trigger life cycle hook ngOnInit to init the component

After you navigate away from your component component will get destroy using ngOnDestroy to avoid memory leak.

Upvotes: 1

Related Questions