1

We are working on Angular migration projects. For that, I have created a Angular 7 app with login & home page. After the successful login of the user login component pass the value to home component. And it is huge data like Page Authorization, roles, Business Units [Around 166 KB]. And service was written in someone.

  1. I call the service to validate the user and the service returning the data (which means successful login).
  2. I'm using following lines of code to redirect to home page with data

    let navigationExtras : NavigationExtras = {
      queryParams : { 
        "data": JSON.stringify(data)
      }
    };
    this.route.navigate(['/home'],navigationExtras); 
    
  3. In home page following used to get the data

    this.route.queryParams
      .subscribe(params => {
        this.data= JSON.parse(params["data"]);
        this.lstUserBusinessUnitDetails = this.data.lstUserBusinessUnitDetails;
      });
    

Problem is am getting the data in URL and when I refresh the page getting The connection was reset error. URL looks like below: http://localhost:4211/home?data=%7B%22lstUserCompanyDetails%22:%5B%5D,%22lstU....

Please advice.

Vadi
  • 2,919
  • 2
  • 11
  • 29

2 Answers2

0

After any refresh of the page all the component of Angular project are destroyed and reinitialize again, at this point all data are gone and you haven't any information from LoginComponent

To resolve this problem, you need to use SessionStorage to save this data on browser and this data will gone when you close this page (not refresh) or you can use LocalStorage and the difference between those is a SessionStorage delete data after delay.

to use session storage:

  • Save data
    sessionStorage.setItem('key', value);
  • Get data
    sessionStorage.getItem('key');

for more information about session storage you can read this document MDN web docs : Window.sessionStorage

Med Aziz CHETOUI
  • 130
  • 2
  • 13
0

if you are using Angular 7.2 and if you trying to pass data between routes , you could make use of the latest feature provide by Angular. Refer this link.

https://netbasal.com/set-state-object-when-navigating-in-angular-7-2-b87c5b977bb

Franklin Pious
  • 3,360
  • 3
  • 23
  • 29