I've got an Angular route resolver that has a list of items that gets loaded when my parent route gets loaded, like /users
. I have implemented an Angular resolver that looks more or less like this:
export class UsersResolver implements Resolve<UsersResponse> {
constructor(private usersService: UsersService) {}
resolve(): Observable<UsersResponse> {
return this.usersService.getUsers();
}
}
The child components consume that resolver data in the standard way:
this.route.data.subscribe((data) => {const users = data['users']} )
That works fine...until I perform any action that might change that list (add/delete/alter a user). In that case, I want that resolver to re-run. Well, more specifically, I want the data returned by the resolver to reflect the updated data. I don't think the static options on runGuardsAndResolvers
will do the trick, nor do I think in my case that using the function you can hand to runGuardsAndResolvers
will work in my case, either, because these data changes might not happen in concert with a route change.
I think what I want is something along the lines of this:
export class UsersResolver implements Resolve<UsersResponse> {
constructor(private usersService: UsersService) {}
resolve(): Observable<UsersResponse> {
return this.usersService.users$;
}
}
...where this.usersService.users$
is an rxjs Subject
. That would mean I could update that subject via other means and (I think) the data in the resolver would update, and therefore any child component subscribed to that resolver would get updated. But if that's right, then where do I trigger the initial data fetch, and how do I ensure that my resolver doesn't actually resolve until the initial data fetch completes?
Is this a common issue with a reasonable solution, or am I going about this in the wrong way?