1

On my homepage I have a component that gets the top 3 latest posts from an API call:

<recent-posts></recent-posts>

Inside the .ts file I get the posts like so:

posts: Object;

const().......

this.posts = this.route.snapshot.data.data;

The call is made in the resolve:

path: '',
component: HomeComponent,
pathMatch: 'full',
resolve: { 
  data: RecentPostResolver
} 

Which has this code:

 resolve(): Observable<any> {
    return this.d.getItems('story', {limit: 3});
}

I want to be able to re-use this component on another page which will then get 6 latest posts.

I bound a parameter to a limit on the component like so

<recent-posts [limit]="6"></recent-posts>

and int he .ts for that component I added:

@Input() limit;

and in the ngOnit() to console log out the limit and whatever value I put in, it shows so that bit is working.

My questions is, How do I pass that passed in value to the Resolve so I can re-use this component to on many pages with different values?

Narm
  • 10,677
  • 5
  • 41
  • 54
modusTollens
  • 397
  • 7
  • 23
  • 1
    seems that you need a parametric resolver, take a look here: https://stackoverflow.com/questions/50384242/resolver-with-parameter-on-resolve-method – ale May 10 '19 at 14:48
  • Hey @Alessandro yes I saw that I could add data to the resolver. How do I get the limit set in the < recent-posts limit='3' > into that data. I am a little noobie so dont crucify apprentice. – modusTollens May 10 '19 at 14:54
  • After much googling it seems impossible to pass data from the component to the resolver. My next idea is to get all data from the resolver and filter in the component. – modusTollens May 10 '19 at 15:01
  • if fact you should not, your component seems to retrieve the post items from the router snapshot, which it is hydrated by your resolver, so it is the resolver that need to read the limit parameter from route data, and so you component remain agnostic – ale May 10 '19 at 15:03

0 Answers0