Ionic4: Fire resolver conditionally

Hello,

I have an app which has 3 components and the router config is as shown below:

export const routes: Routes = [
  {
    path: '', redirectTo: 'Home', pathMatch: 'full'
  },
  {
    path: 'Home', component: HomeComponent
  },
  {
    path: 'EmployeeList', component: EmployeeListComponent,resolve: { BasicRouteInfo: ListResolverService }
  },
  {
    path: 'Employee/:Id', component: EmployeeEditComponent,resolve: { BasicRouteInfo: EditResolverService }
  }
];

And the resolver for EmployeeList Component looks like this:

export class ListResolverService  implements Resolve<any> {

  constructor(public EmpAPISrvc: EmployeeAPIService) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> | Promise<any> | any {
    return new Promise((resolve, reject) => {
        this.EmpAPISrvc.GetEmployeesList().subscribe((response: any) => {
          resolve(response);
        },
          (error) => {
            resolve(null);
          });
    });
  }


}

I have resolver for Employeeslist and EmployeeEdit Components. So before loading the component the resolver will get the data for the component.

From the EmployeeList component user can edit an employee. I want the Employeelist page to be cached so i navigated to Employee page like this:

this.navCtrl.goForward('/Employee/15');

From the employee edit page if user goes back then Employeelist component is loaded. Since i have cached the page i don’t want to make an API call which gets the Employees.

But the resolver fires and gets the employees list even if the page is cached.

Can somebody please tell me how can i stop the resolver from making an API call if the page is already present in stack?

Thank you,
Abhilash

Don’t do any heavy lifting of data in a page. . Create an EmployeeProvider that requests the data and stores it in the provider. The page then asks the provider for the information. The information only updates when the provider wants to update it. 100 pages could request the same information, no problem, the provider only requests from the database once.