简体   繁体   English

导航离开并返回 Angular 后,有没有办法保留组件的 DOM state

[英]Is there a way to preserve DOM state of the component after navigating away and back in Angular

After researching the issue for a few days I've seem to have reached a dead end.在研究了几天这个问题之后,我似乎已经走到了死胡同。

I'm dealing with, what I assumed, was a fairly common scenario: After user navigates to a certain page and makes some changes (content of an input field, scroll through grid data or even changing some editable cell values ), he navigates to different page.我正在处理,我假设,是一个相当常见的场景:在用户导航到某个页面并进行一些更改(输入字段的内容,滚动网格数据甚至更改一些可编辑的单元格值)后,他导航到不同的页面。 When user returns to original page, all of the changes must remain the same.当用户返回原始页面时,所有更改必须保持不变。

Now, that means that cursor in the aforementioned input field must be in the same place, scroll position in the grid and changed cell values, as well as any other changes user made, must be preserved.现在,这意味着上述输入字段中的 cursor 必须在同一个位置,在网格中滚动 position 并更改单元格值,以及用户所做的任何其他更改,必须保留。

Solutions like storing data in localStorage, using services or even custom reuse strategy will not work, because saving that amount of information contained in multiple sub components will be impossible.像在 localStorage 中存储数据、使用服务甚至自定义重用策略这样的解决方案都行不通,因为保存多个子组件中包含的大量信息是不可能的。

Currently, I'm considering DOM manipulation, where rendered template will be moved to a container in parent component in onDestroy hook, and returned later in onInit .目前,我正在考虑 DOM 操作,其中渲染的模板将在onDestroy钩子中移动到父组件中的容器,并稍后在onInit中返回。 That, of course, is a hail Mary attempt and feels very unnatural.当然,这是玛丽的一次尝试,感觉非常不自然。

I'm hoping someone had (and solved) this problem before because, like I've mentioned, it seams like fairly common use pattern.我希望有人之前已经(并解决了)这个问题,因为就像我提到的那样,它看起来像是相当常见的使用模式。

There is not much option to do this in angular compared to React,与 React 相比,在 angular 中没有太多选择可以做到这一点,

What I can recommend you is, store all the data in "environment" as object and when user routes back populate data from environment.我可以向您推荐的是,将“环境”中的所有数据存储为 object 并在用户路由返回时从环境中填充数据。

Usually I do this, when a component loads I store values in objects.通常我会这样做,当组件加载时,我将值存储在对象中。 When on destroy called all this objects goes to the environment under a one main object.当调用销毁时,所有这些对象都会进入一个主要 object 下的环境。

When oninit calls I am checking if object has values and restoring the page to where it was.当 oninit 调用时,我正在检查 object 是否有值并将页面恢复到原来的位置。

I think you are looking for Angular RouteReuseStrategy, where Angular can cache some routes state, without destroying the component state, so when ever the user visit the same route a cached view will be used, RouteReuseStrategy Docs . I think you are looking for Angular RouteReuseStrategy, where Angular can cache some routes state, without destroying the component state, so when ever the user visit the same route a cached view will be used, RouteReuseStrategy Docs .

You can also check this medium article, implementing the required methods Medium您也可以查看这篇中型文章,实现所需的方法中型

for small amount data we can use services or localstorage as you mentioned for large amount data we can go with对于少量数据,我们可以使用您提到的服务或本地存储对于大量数据,我们可以使用 go

NgRx NgRx

https://dzone.com/articles/angular-app-state-management-with-ngrx https://dzone.com/articles/angular-app-state-management-with-ngrx

I use a service for this purpose, saving and restoring the state of the page from an object stored in the service but i haven't been able to figure out how persist the result of a user file upload where i am holding a user uploaded image Pending file upload to cloud storage.我为此目的使用了一项服务,从存储在服务中的 object 中保存和恢复页面的 state,但我无法弄清楚用户文件上传的结果如何保持用户上传的图像等待文件上传到云存储。 Any ideas on solving this helpful as it seems like the cached file is cleared by angular navigation process解决此问题的任何想法都有帮助,因为 angular 导航过程似乎清除了缓存文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航返回后保留参数的正确方法是什么? - What is a proper way to preserve parameters after navigating back? SessionStorage 有时会在导航离开并返回后丢失 - SessionStorage sometimes lost after navigating away and coming back 使用秋田/状态管理从 Angular 物料表中的详细信息页面导航返回后,维护搜索页面的 state - Maintain state of Search page after navigating back from details page in Angular Material table using Akita/State management Angularjs:如何在返回时恢复到上一个​​视图中运行时加载的DOM元素(保留状态) - Angularjs: how to revert back to the runtime loaded DOM elements in the previous view on going back (preserve state) 导航离开并返回页面Aurelia框架后,幻灯片会发疯,该如何解决? - Slideshow goes crazy after navigating away and back to the page, Aurelia framework, how to fix? 在引导中动态添加选项卡在导航后退后不显示选项卡窗格内容 - Dynamically adding a tab in bootstrap doesn't show tab-pane content after navigating away and back Materialize css 视差图像在导航到不同的路线然后返回后消失 - Materialize css parallax images disappear after navigating away to a different route and then back 在动态dom操作后,如何在浏览器历史记录中保留dom状态? - How do I preserve dom state in the browser history after dynamic dom manipulation? 导航到ComponentB时是否损坏了Angular ComponentA? - Is Angular ComponentA destroyed when navigating away to ComponentB? 导航到使用相同组件的另一条路线时,组件的角度检索状态 - Angular Retrieve state of a component when navigating to another route that uses the same component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM