11

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

7
  • which value you want to set to that myOption Commented May 17, 2018 at 6:28
  • so it should be dynamically assigned to an enum value because i should use the same resolver in multiple places (each time with a different resolve param) and i don't want to duplicate the resolver code. i hope this helps clarify Commented May 17, 2018 at 6:36
  • yes then you can send a value through data, while defining the routes Commented May 17, 2018 at 6:39
  • any advice on how i would do that pls? Commented May 17, 2018 at 6:40
  • yeah, I m adding code Commented May 17, 2018 at 6:40

1 Answer 1

31

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); 
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

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.