我目前有一个容器(有状态)组件,它根据方法中的路由参数(id)调度一个select
和一个动作。这些操作的重点是在我的商店中拥有数据和选定的 id。get
ngOnInit
我很好奇在解析器中调度这些动作是否正确?
感谢您的回复。
我的组件:
@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();
}
}
我的路线:
[{
path: ':id',
component: ContainerComponent
}]
解析器将是:
@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;
}
以及修改后的路线:
[{
path: ':id',
component: ContainerComponent,
resolve: {
store: MyResolver
}
}]
这就是为什么我不确定这是正确的,因为商店将永远是null
。