亲爱的堆栈溢出社区你好,
我正面临一个angular2软件设计问题,我不知道什么是更好的解决方案:
我正面临一个angular2软件设计问题,我不知道什么是更好的解决方案:
在ngOnInit中获取REST数据还是使用解析器?
我从来没有遇到过在ngOnInit方法中获取数据的问题,但现在我听说过解析器,我不知道该使用什么:
ngOnInit() { this.authHttp.get('http://localhost:8080/configuration') .map((response: Response) => <ConfigurationData>response.json()) .subscribe(settings => this.settings = settings); }
VS
@Injectable() export class ConfigurationsResolver implements Resolve<ConfigurationData> { constructor(private authHttp: AuthHttp) {} public resolve(route: ActivatedRouteSnapshot): Observable<ConfigurationData> { return this.authHttp.get('http://localhost:8080/configuration') .map((response: Response) => <ConfigurationData>response.json()); } }
解析器会产生更多代码.额外的课程,申报提供者等.
所以你怎么看?关于此的任何陈述和最佳做法?
解决方法
如果您拥有企业应用程序,最好的方法是使用
RxJs,将您的逻辑保留在服务中并触发路由器葫芦或组件中的服务.
或者更好的东西,更多的代码使用ngrx/store并保持状态中的所有逻辑.
在这两个之间选择你写的,我宁愿在服务中写一切,并保持组件更清洁.