Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2本地存储密钥_Javascript_Html_Angular_Local Storage - Fatal编程技术网

Javascript 角度2本地存储密钥

Javascript 角度2本地存储密钥,javascript,html,angular,local-storage,Javascript,Html,Angular,Local Storage,我正在使用本地存储。在构造函数中,我已经完成了: this.currentData=(localStorage.getItem('currentData')!==null)?parse(localStorage.getItem('currentData')):[] 但这样做我会将数据存储在数组中,我认为这不好 所以,我做到了: constructor(){ if(this.currentData !== null && this.currentData !== undefine

我正在使用本地存储。在构造函数中,我已经完成了:

this.currentData=(localStorage.getItem('currentData')!==null)?parse(localStorage.getItem('currentData')):[]

但这样做我会将数据存储在数组中,我认为这不好

所以,我做到了:

constructor(){
if(this.currentData !== null  && this.currentData !== undefined) {
    for (var i = this.currentData.length - 1; i >= 0; i--) {
        this.currentData = JSON.parse(localStorage.getItem('currentData-' + i));
        console.log('currentData if:' + this.currentData)
    }
} else {
    console.log('currentData else:' + this.currentData)
}
}
但我得到一个错误,
TypeError:无法读取未定义的属性'length'
TypeError:无法读取未定义的属性“length”

要保存数据,请执行以下操作:

saveLocalStorage(todo){
    this.objects.push(todo);
    for (var i = this.objects.length - 1; i >= 0; i--) {
        localStorage.setItem('currentData-' + i, JSON.stringify(this.objects[i]));
    }
}
如何从localStorage获取倍数数据?
有人知道我如何解决这个问题吗?

您应该使用注入构造函数的localstorage

您可能忘记了下面的
关键字

this.currentData = (this.localStorage.getItem('currentData')!==null) ? JSON.parse(localStorage.getItem('currentData')) : [  ];
或者,您可以使用as

user:any[]={name:'abc',id:1};
  valuFromLocalStorage:any{};
  constructor(private localStorageService: LocalStorageService) {
    this.name = 'Angular2';
    this.localStorageService.add('a',this.user);
    console.log(this.localStorageService.get('a')); 
    this.valuFromLocalStorage= this.localStorageService.get('a')
  }

我做了一个服务来处理本地存储。也许这可以帮助你:

import { Injectable } from '@angular/core';

@Injectable()
export class NpStorageService {

  constructor(){
    this.checkIflocalStorageIsAvailable();
  }

  private checkIflocalStorageIsAvailable = () => {
    let type = '';
    try {
      let testKey = '__np_storage_test__' + Date.now();
      type = 'localStorage';
      localStorage.setItem(testKey, 'work');
      localStorage.removeItem(testKey);
      type = 'sessionStorage';
      sessionStorage.setItem(testKey, 'work');
      sessionStorage.removeItem(testKey);
      return true;
    }
    catch(e) {
      console.error('NpStorageService => Cannot find ' + type + ' on this browser.');
      return false;
    }
  }

  get = (key?: string) => {
    return JSON.parse(localStorage.getItem(key));
  }

  set = (key: string, value: any): boolean => {
    try {
      localStorage.setItem(key, JSON.stringify(value));
      return true;
    }
    catch(e) {
      return false;
    }
  }

  remove = (key: string) => {
    return localStorage.removeItem(key);
  }

}

使用$localStorage服务

它允许您轻松地与localStorage对象交互,并在数据更改时调用摘要

您可以像这样轻松地添加/删除变量

$localStorage.myObject = {array: [1,2,3,4]};

如果您只是console.log它,那么this.currentData包含什么?您应该像这样实现for循环以便于理解:for(var i=0;i>this.objects.length;i--)谢谢您的回答,我正在将
localstorage
用于构造函数。我会根据你的答案更新代码,我会模拟数据,但我做不到。当我在localstorage中插入一个新数据时,这个新数据需要有一个唯一的localstorage键谢谢你的回答,但我没有使用AngularJS,我使用的是Angular2。谢谢,我会尝试使用它