【问题标题】:Local storage in ionic 2 errorionic 2 错误中的本地存储
【发布时间】:2017-03-14 14:24:55
【问题描述】:

我尝试在我的 Ionic 2 应用程序中使用它: https://ionicframework.com/docs/v2/storage/

我已经跑了

cordova plugin add cordova-sqlite-storage --save

npm install --save @ionic/storage

成功了。

当我尝试在我的 app.module.ts 中添加存储时,我遇到了这个错误:

Error: Can't resolve all parameters for Storage: (?).
    at v (http://localhost:8100/build/polyfills.js:3:4864)
    at SyntaxError.BaseError [as constructor] (http://localhost:8100/build/main.js:127193:27)
    at new SyntaxError (http://localhost:8100/build/main.js:11660:16)
    at CompileMetadataResolver._getDependenciesMetadata (http://localhost:8100/build/main.js:27183:31)
    at CompileMetadataResolver._getTypeMetadata (http://localhost:8100/build/main.js:27058:26)
    at CompileMetadataResolver._getInjectableMetadata (http://localhost:8100/build/main.js:27046:21)
    at CompileMetadataResolver.getProviderMetadata (http://localhost:8100/build/main.js:27288:40)
    at http://localhost:8100/build/main.js:27246:49
    at Array.forEach (native)
    at CompileMetadataResolver._getProvidersMetadata (http://localhost:8100/build/main.js:27213:19)
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:8100/build/main.js:26897:50)
    at JitCompiler._loadModules (http://localhost:8100/build/main.js:72991:64)
    at JitCompiler._compileModuleAndComponents (http://localhost:8100/build/main.js:72951:52)
    at JitCompiler.compileModuleAsync (http://localhost:8100/build/main.js:72917:21)
    at PlatformRef_._bootstrapModuleWithZone (http://localhost:8100/build/main.js:52753:25)

我不明白我该怎么做才能解决它。

我的 app.module.ts :

import { Storage } from '@ionic/storage';
...

providers: [
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    PData,
    PBackground,
    PTranslate,
    Storage
  ]

...

【问题讨论】:

    标签: android ios cordova ionic2


    【解决方案1】:

    从 Ionic 2.2.0 开始,建议使用 @ionic/storage 2.0.0 版本。 app.modules.ts 中的配置自上一个版本以来发生了变化。如果您没有以正确的方式更改所有内容,则会发生错误。

    在 app.modules.ts 中进行以下更改:

    1. 从提供程序中删除存储
    2. 更改导入语句:

      来自:import { Storage } from '@ionic/storage';

      收件人:import { IonicStorageModule } from '@ionic/storage';

    3. 将以下内容添加到导入数组:

      IonicStorageModule.forRoot()

    导入数组应如下所示:

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



    注意:请勿对任何其他文件中的 Storage 导入进行任何更改。

    【讨论】:

    • 非常感谢!如果他们更新文档会更好! ^^'
    • @V.Pivet 他们做到了!有文档可供阅读大声笑您可以查看与存储herehere 相关的更改。在更新日志中,他们明确地说:Note: If you are using ionic-storage, you need to update it to 2.0.0 or you will run into an error similar to this: Error: Can't resolve all parameters for Storage: (?, ?).. For more information, see the Storage Documentation.
    【解决方案2】:

    您的 app.module.ts 应如下所示,

    import { Storage } from '@ionic/storage';
    
    export function provideStorage() {
      return new Storage(['sqlite', 'websql', 'indexeddb'], { name: 'database_name' });
    }
    
    @NgModule({
      declarations: [
      ],
      imports: [
        IonicModule.forRoot(MyApp)
      ],
      bootstrap: [IonicApp],
      entryComponents: [
      ],
      providers: [
        { provide: Storage, useFactory: provideStorage }, Storage
      ]
    })
    

    您必须使用 set()get() 方法来存储和检索页面中的数据。看看Ionic 2 Storage Tutorial Blog with example video。希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2018-08-02
      • 2017-10-02
      • 2018-03-19
      相关资源
      最近更新 更多