【问题标题】:Ionic storage not working in chrome browser离子存储在 Chrome 浏览器中不起作用
【发布时间】:2018-05-26 23:50:08
【问题描述】:

我是 Ionic 应用程序开发的新手,我一直在学习相同的教程。

我一直在尝试使用IonicStorageModule,但即使在 chrome 浏览器移动视图模式下运行应用程序时没有错误,也不会生成本地存储密钥。

在我的app.module.ts,我有

import { IonicStorageModule } from '@ionic/storage';

然后我在imports数组中有以下导入,

  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()
  ],

现在,我创建了一个服务,命名为user-settings.service.ts

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';

@Injectable()
export class UserSettings {
    constructor(private storage:Storage){}

    favoriteTeam(team,tournamentId, tournamentName) {
        debugger;
        let item = { team: team,tournamentId : tournamentId, tournamentName : tournamentName};
        this.storage.set('teamId' , item);
    }

    unFavoriteTeam(team) {
        this.storage.remove(team.id);
    }

    isFavoriteTeam(team) {
        return this.storage.get(team.id).then(value => value ? true : false);
    }
}

当我这样做时,从我的组件类中......

}else {
  this.isFollowing = true;
  this.userSettings.favoriteTeam(this.team,this.currentTournamentData.tournament.id,this.currentTournamentData.tournament.name);
}

即使服务被击中,我也看不到在 chrome application -> storage -> local storage. 中创建 local stoarge

我做错了什么?

【问题讨论】:

  • 检查你使用的是mac而不是隐私浏览模式?
  • @DeepakJha 不是隐私浏览模式,但我肯定在使用 mac。
  • 好吧,我曾经遇到过同样的问题,实际上苹果云不允许存储来自网络的任何内容,但我还记得当时我遇到了一个错误,显示为“达到最大堆栈大小”我可能在我的错误陈述中是不正确的,但它接近那个。如果允许您将系统中的任何内容存储在 applecloud 中,我建议您也搜索此选项。

标签: angular typescript ionic2 ionic3


【解决方案1】:

就像您在 the docs 中看到的那样:

当在 Web 中运行或作为 Progressive Web App 运行时,Storage 将 尝试按顺序使用 IndexedDBWebSQLlocalstorage

因此,如果您使用的是 Chrome,请先检查数据是存储在 IndexedDB 还是 WebSQL 中:

另外请注意,您正在使用teamId 键设置数据,如下所示:

this.storage.set('teamId' , item);

因此,为了取回该数据,您需要使用相同的密钥:

this.storage.get('teamId').then(value => value ? true : false);

如果你想从存储中删除它也是一样的:

this.storage.remove('teamId');

【讨论】:

  • 亲爱的投票者,您能解释一下您投票的原因吗?这样我们都可以学习:)
  • 我认为StackOverflow 必须提供该功能。 没有人可以在不提及原因的情况下投反对票。你的想法?
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 2016-11-30
  • 2017-11-11
相关资源
最近更新 更多