1

I'm making an agular app which load data from an API, and when I go to another route and go back to table route the data is not there.

Is there some way to change route and not losing data?

<button [routerLink]="['/home']">
   <i class="bx bx-left-arrow-alt" id="icone"></i>
</button>

Now I'm just using a routerLink to change routes.

And I'm using angular 15.

4
  • Same question asked here a few years ago but it has some fresh answers. you can check it Commented Dec 26, 2022 at 12:40
  • Have you tried Resolver? please insert your component ts file here
    – h.zare
    Commented Dec 26, 2022 at 12:41
  • No, How it works?
    – Hugo vilar
    Commented Dec 26, 2022 at 12:42
  • I've seen this question but it didn't keep the data in table
    – Hugo vilar
    Commented Dec 26, 2022 at 12:51

2 Answers 2

0
import {Component} from '@angular/core';
import {Location} from '@angular/common';

@Component({selector: 'app-second-part',
templateUrl: './second-part.component.html',
styleUrls: ['./second-part.component.css']})
class SecondPartComponent {

 constructor(private location: Location){}

 goBack() {
  this.location.back();
 }
}
1
  • 1
    this way helps you if you want to keep your query params data.
    – h.zare
    Commented Dec 26, 2022 at 13:00
0

this is a resolver file

export class ReadyLinesResolver implements Resolve<any> { 

 resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any {
   // return your API response
 }  
}

this is your router:

{
   path: '',
   resolve: {userInfo: UserInfoResolver},
},

you must inject the Router module in the constructor.

this is your ngOnInit component:

this.router.data.subscribe(res=> {
  // check here and find your property name
})

return your data in a variable and use them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.