我有两个提供者。
AppStorage 负责从存储中设置和获取值。
import { Injectable } from '@angular/core'; import {Http, Headers, RequestOptions} from '@angular/http'; import {Observable} from 'rxjs/Rx'; import {Storage} from '@ionic/storage'; @Injectable() export class AppStorage { constructor(public http: Http, private storage:Storage) { console.log('Hello Appstorage Provider'); } setValue(key,value){ var setPromise = new Promise((resolve,reject)=>{this.storage.set(key,value).then((res)=>{ return resolve(res); }) }); return setPromise; }; getValue(key){ var getPromise = new Promise((resolve,reject)=>{this.storage.get(key).then((val)=>{ return resolve(val); }); }); return getPromise; } }
OauthService 这是我的 api 服务。
import {Http, Headers, RequestOptions} from '@angular/http'; import { Injectable, Inject} from '@angular/core'; import 'rxjs/add/operator/map'; import { AppStorage } from '../providers/appstorage'; @Injectable() export class OauthService{ http: Http; response :any; appStorage: AppStorage; static get parameters() { return [[Http]]; } constructor(http: Http,@Inject(AppStorage) appStorage: AppStorage){ this.http = http; this.appStorage = appStorage; setTimeout(()=>{ console.log('Hello OauthService Provider'+this.appStorage+"===="+this.http); },3000) //output - >Hello OauthService Providerundefined====[object Object] } }
现在我将两个可注射添加到我的 app.module
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { LoginPage } from '../pages/login/login';
import { AppStorage } from '../providers/appstorage';
import { Storage } from '@ionic/storage';
import { OauthService } from '../providers/oauthservice';
@NgModule({
declarations: [
MyApp,
LoginPage
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
LoginPage
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},OauthService, Storage, AppStorage]
})
export class AppModule {}
但是当我运行应用程序时,oauthService 的构造函数中的 appStorage 对象未定义。你能告诉我我在做什么错误吗,我只是在另一个服务中注入服务。