0

Is there any way to prevent resolve on router if only parameters on that router are changed?

For example, I have this route config.

{path:'home/:status', component:HomeComponent,pathMatch:'full',resolve:{ actionList : HomeActionListResolve}}

The resolve here returns full data set and the component just needs to filter the data based on status like (active, completed, etc)parameter.

What i am trying to achieve is that, if the user is landing on this URL for first time, then resolve should be called, so that it can fetch data.

But if user is changing only status, then resolve should not be called OR even if it is called, then it should not make HTTP request to get the same data again and again.

Can someone please suggest a way to achieve this?

Thanks for the help in advance.

2
  • are you saving the status info somewhere in service Commented Jul 4, 2017 at 12:47
  • @RahulSingh, No I am not storing. It will be appended in the URL itself and component will retrieve it and set it a property for usage. Commented Jul 4, 2017 at 12:53

1 Answer 1

1
@Injectable()
export class ResolveFactory implements Resolve<any> {

  url: string[];

  constructor() {

  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any>|Promise<any>|any {
    // get the snapshot from activatedroutesnapshot
    let urlStatus= route.url[0].path; 
    or
    let urlStatus = state.url; 

    if(url.contains(split url to get the status){
         return false;
      } else {
        do your work call service and add the status to the URL[] in Resolve
      }
    }
  }
Sign up to request clarification or add additional context in comments.

6 Comments

So you suggesting not to make HTTP calls if there is status in URL and return false. Am i right ??
@SameerK you can do what ever you want there nd after each service call or in else add the status to the url array
Thanks, but the moment i inject Route into the resolve, i get the following error TS24220: class 'HomeActionListResolve' incorrectly implements the interface 'Resolve<any>'. Types of property 'resolve' are incompatible. Type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot, router: Route) => any' is not assignable to type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => any'.
please remove the route from the constructor call get the snapshot from activated route snapshot updated the answer my bad
Now this error property 'value' does not exist on type 'UrlSegment[]' for let urlStatus= this.route.url.value.map(val => val.path);. any idea ??
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.