Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Keep state of previous page in Angular 10

im reading data in component A, route to component B and pass data as extras.
Component B now has this data in its instance scope. I then pass this data to component C and do something with that data. If I now return to component B by going back a page, the data which I received from A (my state) is not present anymore.

My question: How can I keep the state of B?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can store your data in a shared service, as these are singleton classes, the data will persist until your application is destroyed.

@Injectable({
  providedIn: 'root',
})
export class MyService {
  data = '';
}
export class OneComponent implements OnInit {
  constructor(private myService: MyService){};

  ngOnInit(): void {
    this.myService.data = "I'm not going anywhere!"
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading