Home > Software engineering >  Making a button to go back page
Making a button to go back page

Time:12-27

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  id="icone"></i>
</button>

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

And I'm using angular 15.

CodePudding user response:

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();
 }
}

CodePudding user response:

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

  • Related