I'm currently have a container (stateful) component which dispatches a select
and a get
action based on a route param (id) in the ngOnInit
method. The point of these actions to have the data and the selected id in my store.
I'm curious would it be correct to dispatch these actions in a resolver?
Thanks for the replies.
My component:
@Component({
selector: 'app-container',
templateUrl: './container.component.html',
styleUrls: ['./container.component.css']
})
export class ContainerComponent implements OnInit, OnDestroy {
private componetDestroyed$ = new Subject();
constructor(private store: Store<fromRoot.State>, private route: ActivatedRoute) { }
ngOnInit() {
this.route.params
.filter(params => params['id'])
.map(params => params['id'])
.takeUntil(this.componetDestroyed$)
.subscribe(id => {
this.store.dispatch(new GetAction(id));
this.store.dispatch(new SelectAction(id));
});
}
ngOnDestroy() {
this.componetDestroyed$.next();
this.componetDestroyed$.unsubscribe();
}
}
My routes:
[{
path: ':id',
component: ContainerComponent
}]
The resolver would be:
@Injectable()
class MyResolver implements Resolve<any> {
constructor(private store: Store<fromRoot.State>) {}
resolve(route: ActivatedRouteSnapshot, state: RouteStateSnapshot) {
let id = route.params['id'];
this.store.dispatch(new SelectAction(id));
this.store.dispatch(new GetAction(id));
return null;
}
And the modified routes:
[{
path: ':id',
component: ContainerComponent,
resolve: {
store: MyResolver
}
}]
And that's why I'm not sure this is correct, becuase the store will always be null
.