Angular2全球数据模型?
关于使用全局变量和全局状态的Angular2,有几个问题和讨论,但我的特殊需求略多于此,我正在努力寻找最佳方法(主要是因为在Angular2发布周期的不同时间发布了相互冲突的信息) 因此,我实际上是在创建一个SPA(单页应用程序),作为销售办公室的订单输入屏幕 我的结构有点像这样:Angular2全球数据模型?,angular,rxjs,angular2-services,Angular,Rxjs,Angular2 Services,关于使用全局变量和全局状态的Angular2,有几个问题和讨论,但我的特殊需求略多于此,我正在努力寻找最佳方法(主要是因为在Angular2发布周期的不同时间发布了相互冲突的信息) 因此,我实际上是在创建一个SPA(单页应用程序),作为销售办公室的订单输入屏幕 我的结构有点像这样: |_main_order_entry controller.ts |__order_line_controller.ts |_product_controller.ts |
|_main_order_entry controller.ts
|__order_line_controller.ts
|_product_controller.ts
|_colour_controller.ts
|_size_cotroller.ts
|_promotion_controller.ts
|_comments_controller.ts
{
"orderdate": "01/01/2017",
"orderef": "JHASGAZXC",
"orderlines": [
{
"linenumber": "1",
"product": "T-SHIRT",
"colour": "BROWN",
"size": "Large",
"promotion": "True",
"comments": "Customer has requested gift wrapping"
},
{
"linenumber": "2",
"product": "JEANS",
"colour": "BLUE",
"size": "X-SMALL",
"promotion": "False",
"comments": "None"
}
]
}
因此,当您加载主订单条目\u controller.ts时,您可以预先填充一些基本数据、顺序中的下一个订单号以及订单日期/时间等简单的内容
然后我们从一个订单行控制器开始,比如说,在这里事情变得有点棘手
main_order_model.ts
order_line_model.ts
然后创建一个服务,每次加载main\u order\u entry\u controller.ts时,它都会创建一个main\u order\u model.ts的新实例,然后为每个加载的order\u line\u controller创建一个order\u line\u model.ts的新实例
给我一个数据结构有点像这样的服务:
|_main_order_entry controller.ts
|__order_line_controller.ts
|_product_controller.ts
|_colour_controller.ts
|_size_cotroller.ts
|_promotion_controller.ts
|_comments_controller.ts
{
"orderdate": "01/01/2017",
"orderef": "JHASGAZXC",
"orderlines": [
{
"linenumber": "1",
"product": "T-SHIRT",
"colour": "BROWN",
"size": "Large",
"promotion": "True",
"comments": "Customer has requested gift wrapping"
},
{
"linenumber": "2",
"product": "JEANS",
"colour": "BLUE",
"size": "X-SMALL",
"promotion": "False",
"comments": "None"
}
]
}
然后每次更改控制器值时,它都会更新全局服务中的数据模型,理论上每个控制器都可以从服务中提取所需的任何数据
我对Angular2还比较陌生,所以我正在寻找正确的方法,这周我在gitter官方频道上四处打听,运气不太好,非常感谢。要管理全球状态,你可以使用ngrx/store和ngrx/effects。这两个实现了Redux模式,这是管理应用程序状态的一种很好的方法,因此您可以有一个真实来源来存储数据,并在应用程序的整个生命周期内共享数据
首先尝试学习redux模式。然后要学习ngrx,请访问。如果您需要问一些问题,可以转到非常感谢,当我编写了反映我的问题的代码时,我会在中添加一个答案,以显示一个有效的答案,这正是我想要的。