角度4:路由到另一个组件后保存组件状态 [英] Angular 4: Save component state after routing to another component

查看:89
本文介绍了角度4:路由到另一个组件后保存组件状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular开发一个应用程序,目前我有这个问题:

I'm developing an application with angular and currently I have this problem:

我有一个组件,用户可以在其中填充三个选择框.之后,他可以开始搜索以找到匹配的匹配项.现在,他们可以单击一个命中并显示详细信息.详细信息显示在第二个组件中.从组件1到组件2的转换通过a

I have a component in which the user can fill three selectboxes. After that he can start a search to find the matching matches. Now they can click on a hit and have the details displayed. The details are displayed in a second component. The transition from component 1 to 2 succeeds via a

[routerLink]="..."

我的问题是,当我回到概览时,必须重新输入所有数据.如何保存第一个组件的状态,以便用户不必再次填写三个选择框?

My problem is when I go back to the overview, all data must be re-entered. How can I save the state of the first component so that the user does not have to fill out the three selectboxes again?

推荐答案

1方法是将状态存储在本地存储中.因此,当您返回原始页面时,可以查询该项目是否存在于本地存储中(如果确实存在的话),然后重新初始化.

1 method would be to store the state in the local storage. So when you return back to the original page you can query to see if the item exists in the local storage if it does then reinitialise.

let data = {};
localStorage.setItem('settings', data);

或者,您也可以通过路由器传回比赛和第四次比赛 参数.

Alternatively you can pass in the matches back and fourth via router parameters.

https://angular.io/guide/router

这篇关于角度4:路由到另一个组件后保存组件状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆