【问题标题】:Storage injection problems in ionic离子中的存储注入问题
【发布时间】:2018-05-27 06:56:19
【问题描述】:

当我在用户服务中注入存储服务时出现错误。我拿了一个项目模板,我自己看不到错误。

文件:app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platfoenter code hererm-browser';
import { IonicStorageModule, Storage } from '@ionic/storage';
import { IonicApp, IonicModule } from 'ionic-angular';
import { HTTP } from '@ionic-native/http';
import { Settings } from '../providers/providers';
import { User } from '../providers/providers';
import { Api } from '../providers/providers';
import { MyApp } from './app.component';

export function provideSettings(storage: Storage) {
  return new Settings(storage, {
    option1: true,
    option2: 'Ionitron J. Framework',
    option3: '3',
    option4: 'Hello'
  });
}

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    Api,
    HTTP,
    User,
    { provide: Settings, useFactory: provideSettings, deps: [Storage]}
})
export class AppModule { }

文件:user.ts

import { Injectable } from '@angular/core';
import { Api } from '../api/api';
import { Storage } from '@ionic/storage';
import { HTTP } from '@ionic-native/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class User {
  username: string;
  customerId: string;

  constructor(public api: Api, public http: HTTP, private storage: Storage) { }

  /**
   * Use Basic authentication and send a request to our endpoint to get an id
   */
  login(username: string, password: string){
    return new Observable<void>(observer => {
      this.api.me().then(
      (data) => {
        this.storage.set('CustomerId', data['customerId']);
        observer.next();
      },
      error => {
        console.error('ERROR', error);
        observer.error(error);
      });

    });

  }

我得到的错误是:

错误:未捕获(承诺):错误:StaticInjectorError[Storage]: 静态注入器错误[存储]: NullInjectorError:没有存储提供者! 错误:StaticInjectorError [存储]: 静态注入器错误[存储]: NullInjectorError:没有存储提供者! 在 _NullInjector.get (http://localhost:8100/build/vendor.js:1276:19) 在 resolveToken (http://localhost:8100/build/vendor.js:1564:24)

【问题讨论】:

    标签: ionic-framework ionic3


    【解决方案1】:

    您需要在@NgModule 中提供存储空间

    import { Storage } from '@ionic/storage';
    
    
    @NgModule({
      ...
      providers: [
       ...
        Storage,
        ...
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多