如何在角2中保存组件的状态? [英] How can we save state of component in angular 2?
问题描述
我知道很多人已经问过这个问题,但是没有一个人给出明确的答案.我只想存储组件的状态,当它导航到另一个组件然后返回到上一个组件时.我知道我们可以将数据保存在服务中,然后将数据提取回组件.但是,我认为这很容易,但是当组件过于复杂时,并不是更好的解决方案.我读了一些路由器重用的东西,但是没有一个能在角度2中工作.
I know lots have already asked this question but none of them gave clear answer. I just want to store the state of component when it navigates to another and then come back to previous one. I know we can save the data in service and then fetch the dats back to component. But, I think this is easy but not the better solution when the component is too complex. I read some router reuse things but non of them work in angular 2.
推荐答案
有几种选择:
1)使用服务.如果数据很复杂,这实际上是最好的方法之一.我在这里有一个简单的示例: https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/
1) Use a service. This is actually one of the best ways if the data is complex. I have a simple example here: https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/
2)路由参数.您可以指定必需,可选或查询参数.请参阅此以获取更多信息:通过route.navigate发送数据在Angular 2中
2) Routing parameters. You can specify required, optional, or query parameters. See this for more information: Sending data with route.navigate in Angular 2
如果您正在寻找有关路由器重用策略的信息,则可以在此处找到一些信息: https://medium.com/@juliapassynkova/angular-2-component-reuse-strategy-9f3ddfab23f5
If you are looking for information on the router reuse strategies, you can find some information here: https://medium.com/@juliapassynkova/angular-2-component-reuse-strategy-9f3ddfab23f5
这篇关于如何在角2中保存组件的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!