刷新后将数据保留在页面中 [英] Keep data in page after refresh
问题描述
我的角度项目中有2页。其中一个是主页面。另一个是弹出页面。我在弹出页面中输入数据输入标签,然后当我点击添加按钮时,数据被添加到主页面。它运行良好。我在主页面中保存数据一个数组。但是如果我刷新页面(主页面)数据似乎不再存在。你有什么想法吗?
I have 2 pages in my angular project.One of them is main page.The other one is popup page. I enter data to input tags in my popup page,then when i click the add button data is added to main page.It is working nicely.I hold data an array in my main page.But if i refresh the page (main page) data does not seem anymore.Do you have any idea?
我做了一些研究并学习了本地存储和cookies。但我可以将数据从页面发送到其他页面。我想做的是将数据保存在页面中。如何处理?
I made a some researches and have learned localstorage and cookies.But i can send data from a page to other page.What i want to do is keep data in page.How can i handle it?
我的理解是我们使用localstorage将数据发送到另一页。我是对的?
What i understand is we use localstorage to send data another page.Am i right?
推荐答案
这可以通过localStorage来完成。
This can be done with a localStorage.
一个例子:
localStorage.removeItem('Array');
localStorage.setItem('Array', JSON.stringify(this.array));
以上是如何保存它以便在 ngOnInit <中执行此操作/ strong>:
The above is how you save it to get it back you do this in the ngOnInit:
this.array = JSON.parse(localStorage.getItem('Array'));
localStorage.removeItem('Array'); // to clear it again.
编辑
导入:
从'@ angular / core'导入{OnInit};
在您的组件(类)上实现它:
Implement it on your component(class):
export class YourClass实现OnInit
构造函数之后:
ngOnInit() { // your code here }
这篇关于刷新后将数据保留在页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!