How to convert an HTML local storage service to an indexedDB service in Angular 2?

时间:2017-06-12 17:06:52

标签: html5 angular local-storage indexeddb

I have an angular2 service that employs LocalStorage to store the relevant data for my application.

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

@Injectable() 

export class LocalStorageService {

  get(item) {
    return JSON.parse(localStorage.getItem(item));
  }

  set(target, item) {
    localStorage.setItem(target, JSON.stringify(item));
  }

  remove(item) {
    localStorage.removeItem(item);
  }
}

As the data grew for the application(over 15 MB now), I decided to use IndexedDB v1.0 instead of LocalStorage. Can somebody help me redefine my get(), set() and remove() functions, keeping in mind that I want the methods to be synchronous,(especially, the get() and remove() methods)?

1 个答案:

答案 0 :(得分:0)

indexedDB方法是异步的。我建议不要打败一场失败的战斗。