繁体   English   中英

Ionic本地存储删除项目

[英]Ionic local storage remove item

有人可以帮助我创建一种从离子本地存储中删除的方法吗?

到目前为止,我已经尝试过

    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    get: function(key) {
      return $window.localStorage[key];
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
      return JSON.parse($window.localStorage[key]);
    },
    removeItem: function(key){
      $window.localstorage.splice(key, 1);
    }

removeItem根本不起作用。 我想按职位而不是按键删除。

您正在使用localStorage作为数组,但没有使用它。 它具有删除项的默认功能:

removeItem: function(key){
    $window.localStorage.removeItem(key);
}

如果要按索引删除,则必须先获取该项目:

removeByIndex: function (index) {
    $window.localStorage.removeItem($window.localStorage.key(index));
}

尝试使用内置方法,这将有助于完成从LocalStorage删除key:value的整个事务。

https://auth0.com/docs/native-platforms/ionic#8

这将是最好的方法。 使用此工厂,您可以创建,检索或删除任何已创建的密钥

.factory('sessionService',['$http',function($http){
  return {
     set:function(key,value){
     return localStorage.setItem(key,JSON.stringify(value));
   },
   get:function(key){
     return JSON.parse(localStorage.getItem(key));
   },
   destroy:function(key){
     return localStorage.removeItem(key);
   },
 };
}])

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM