I have api calls in the component which is time taking, so i have implemented resolver. I want the resolver data to be present in the store, for later use. I have implemented ngrx/store, ngrx/effects and ngrx/router-store.
Current state in component
- getting data directly from resolver data in component (route is of type ActivatedRoute)
Desired state
To keep the resolver data in store, I need to access resolver data in effects. So i can simply dispatch action in component and subscribe the state.
- dispatch action (LoadSubTopicDetails) in component
- effects will listen to the action and access the same resolver data in effects
Problem
I am always getting empty {}
in "data" in CustomSerializer.
using resolver in app-routing.module.ts
console output when routed to SubTopic/:subTopicId
How to access resolver data in effects? Thanks in advance.
Code
reducers / index.ts (CustomSerializer)
export interface RouterStateUrl {
url: string;
queryParams: Params;
params: Params;
data: any;
}
export const reducers: ActionReducerMap<State> = {
routerReducer: fromRouter.routerReducer
};
@Injectable()
export class CustomSerializer implements fromRouter.RouterStateSerializer<RouterStateUrl> {
serialize(routerState: RouterStateSnapshot): RouterStateUrl {
let route = routerState.root;
while (route.firstChild) {
route = route.firstChild;
}
const { url, root: { queryParams } } = routerState;
const { params, data } = route;
console.log('routerData in CustomSerializer', { url, queryParams, params, data });
return { url, queryParams, params, data };
}
}
reducers / router.selector.ts
export const getRouterState = createFeatureSelector< RouterReducerState<RouterStateUrl> >('routerReducer');
component.ts
// current state of component
this.subTopicDetails = this.route.snapshot.data['content'];
// new way (desired state), getting "resolver data" from store
this.store.dispatch(new fromCourse.LoadSubTopicDetails);
this.store.select(getSubTopicDetails)
.subscribe(data => this.subTopicDetails = data);