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
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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With