0

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?

1 Answers1

2

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!"
  }
}
Chris Hamilton
  • 9,252
  • 1
  • 9
  • 26
  • Thanks a lot, I should have mentioned that for my purpose, writing a service class would be a bit "too heavy". I will try the localStorage approach, but I will accept this as answer, because I think this would be the better approach. – Karsten Hertenstein Apr 11 '22 at 07:45