Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resolver with parameter on resolve method

So I implemented a resolver in angular 5:

@Injectable()
export class AppResolver implements Resolve<MyComplexObject []> {
  constructor(private myService: MyService) {

   }
   resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<MyComplexObject[]> {
     return this.myService.getMyApi(myOption); // my option is a string
   }
}

right now myOption is a hardcoded string and i want to change that

in my routing module i have:

resolve: {
  myResolver: AppResolver
}

I suppose maybe here I should specify the value of myOption string but how?

or better yet where I actually call the resolver

this.route.data.map(data => data.myResolver).subscribe(
  result => {
    // do something with the result (specify who the myOption is?? How)
  }
);

the parameter is not necessarily visible in the browser :

it will be part of the url: /.../.../myString/..

but it's not introduced by a param : url: /..&myParam=paramValue

so I can t use myParam to identify it from the url and replace it

like image 865
annepic Avatar asked May 17 '18 05:05

annepic


People also ask

What is the use of resolve object in routing?

Resolvelink A data provider class can be used with the router to resolve data during navigation. The interface defines a resolve() method that is invoked right after the ResolveStart router event. The router waits for the data to be resolved before the route is finally activated.

What is resolver in angular example?

So what is Angular Resolver? Angular Route Resolver is used for pre-fetching some of the data when the user is navigating from one route to another. It can be defined as a smooth approach for enhancing user experience by loading data before the user navigates to a particular component.


1 Answers

Here is an example to send data to resolver,

Route configuration:

{
  path: 'project/:id',
  component: ProjectComponent,
  resolve: { data: AppResolver },
  data: { resolvedata: 'myValue' }
}

Resolver:

@Injectable()
export class AppResolver implements Resolve<MyComplexObject []> { 
  constructor(private myService: MyService, private router: Router) {} 
  resolve(route: ActivatedRouteSnapshot): Observable<MyComplexObject[]>|boolean { 
    let myParam = route.data['resolvedata']; 
    console.log(myParam); 
  } 
}
like image 138
Sravan Avatar answered Oct 22 '22 04:10

Sravan