我创建了服务的本地instance
并直接从中访问数据。如果它是一个简单的用例(在组件之间共享数据),这是不正确的。我试图掌握ng2的一些核心概念,我觉得可能有更好的选择。
例如
export class FormTwoComponent implements OnInit {
private model: WinnerModel;
constructor(private formService: FormService, private router: Router) {}
ngOnInit() {
this.model = this.formService.winnerModel;
}
onSubmit() {
this.formService.winnerModel = this.model;
this.router.navigate(['/form/3'])
}
}
由于
答案 0 :(得分:0)
Angular有一个关于component interactions的精彩页面。我觉得有一点不太明确的是,如果你不引用对象,那么组件中的数据将被遗忘。例如:
component.variable = service.variable //this will take the current value of what the service variable is, if the service variable ever changes then the component will not get updated.
component.object = service.object //this will however create a link and any updates or changes made to the service object, then the component reference will be notified and updated.