【问题标题】:How to retrieve the value from LocalStorage如何从 LocalStorage 中检索值
【发布时间】:2019-08-20 00:47:48
【问题描述】:

我正在尝试测试 html5 localStorage 功能。出于某种原因,每当我在刷新页面后尝试从存储中检索一个值时,我都没有得到任何值。值在我的点击功能上本地存储。但是当我刷新页面时,它不显示值。

src/app.component.ts 文件

>import { Component } from '@angular/core';

>export class MyItems {
  >value: string;
  >constructor(value:string){
    >this.value = value;
 > }
>}
>@Component({
  >selector: 'my-app',
  >templateUrl: './app.component.html',
  >styleUrls: [ './app.component.css' ]
>})
>export class AppComponent  {
  >title = "Working with Angular";
  >myItems: MyItems[] = new Array();
  >IsForUpdate: boolean = false;
  >newItem: any = {};
 > updatedItem;

  >constructor(){
    >this.myItems.push(    
      >new MyItems("First Value"),    
      >new MyItems("Second Value"),    
      >new MyItems("Third Value"),    
      >new MyItems("Forth Value"),    
      >new MyItems("Fifth Value")    
    >);
    >localStorage.setItem("Values", JSON.stringify(MyItems));
    >var getValues = localStorage.getItem("Values");

  >}

 >AddItem() {    
   >this.myItems.push(    
    > this.newItem    
   >);    
   >this.newItem = {};
   >localStorage.setItem('dataSource', this.newItem);
   >localStorage.getItem('dataSource');    
  // console.log(localStorage.getItem('dataSource'));
> }  


 >EditItems(i){
   >this.newItem.value = this.myItems[i].value;
   >this.updatedItem = i;
   >this.IsForUpdate = true;
 >}



> UpdateItem(){
   >let data = this.updatedItem;
   >for(let i=0; i < this.myItems.length; i++){
    > if(i == data){
      >this.myItems[i].value = this.newItem.value;
     >}
  > }
    >this.IsForUpdate = false;
   > this.newItem = {};
 >}


 >DeleteItem(i) {
  >var confirmMe = confirm('Do you want to Delete it ?');
  >if(confirmMe == true){
      >this.myItems.splice(i, 1); 
  >} else {
   > alert("You cancelled the Operation");
  >}  

>}  
>}

【问题讨论】:

  • 您对哪个本地存储密钥有问题,ValuesdataSource

标签: angular angular7


【解决方案1】:

如果您尝试在localStorage 中存储数组或对象,则需要将其转换为字符串格式,因为 localStorage 仅支持存储字符串值。为此,您可以使用JSON.stringify()

localStorage.setItem('Values', JSON.stringify(this.newItem));

localStorage.setItem('dataSource', JSON.stringify(this.items));

同样,当您需要从 localStorage 中检索项目时,您可以使用JSON.parse() 将其转换回数组或对象。

const storedItems = JSON.parse(localStorage.getItem('dataSource'));

在您的构造函数中,您填充数组的方式过于复杂。填充 myItems 数组后,您可以将其存储在 localStorage 中。

在您的 addItem() 方法中,您可以简单地将新项目推送到您的 myItems 数组,然后调用 localStorage.setItem(),这将覆盖之前存储在您的 Values 键上的值。

myItems: string[] = [];

constructor(){
  console.log(JSON.parse(localStorage.getItem('Values')));

  this.myItems.push('First Value', 'First Value', 'Third Value', 'Forth Value', 'Fifth Value');
  localStorage.setItem('Values', JSON.stringify(this.myItems));
}

addItem() {
  const newItem = ''; //replace that with any value you desire
  this.myItems.push(newItem)
  localStorage.setItem('Values', JSON.stringify(this.myItems));
}

【讨论】:

  • 但是在我的代码中添加后如何检索项目?你能帮我吗
  • @tkamath99 您指的是AddItem() 方法中的代码吗?好的,你到底想检索什么?
  • 在构造函数中,我将内容添加到一个数组中,该数组显示在一个框中的首页中。所以每当我从 AddItem() 添加一个新项目时,我希望它即使在页面刷新后也能留在那里
  • 好的,this.myItems 包含这个,对吧? ['First Value', 'Second Value', ...]
  • @tkamath99 是的,我明白了。好的,你看到我回答的第二部分了吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-01
  • 2016-08-24
  • 2021-08-04
  • 2013-05-14
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
相关资源
最近更新 更多