0

我有两个供应商。供应商在Ionic2/Angular2中的应用程序

  1. 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; 
    
        } 
    } 
    
  2. 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对象来为未定义。 你能告诉我我正在做什么错误,我只是在另一项服务中注入服务。

+1

而不是'''@注入(AppStorage)appStorage:AppStorage)'''之后'''this.appStorage = appStorage;'''你可以做'''私人appStorage:AppStorage)'''。私人会自动将对象粘贴到此处。不知道这是否会改变你的问题。 –

+0

没有一个人在工作... – RHUL

+0

你在控制台中看到'Hello Appstorage Provider'吗? –

回答

3

你的静态参数吸气(参数)在OauthService提供商应该包括所有你注入到构造的供应商。但是,您只注入了Http提供程序,但不提供AppStorage提供程序。 我已经修改相应的代码如下:

import { Http } from '@angular/http'; 
import { Injectable } from '@angular/core'; 
import 'rxjs/add/operator/map'; 
import { AppStorage } from './appstorage'; 

@Injectable() 
export class OauthService { 

    response :any; 

    static get parameters() { 
     return [[Http], [AppStorage]]; 
    } 

    constructor(private http: Http, private appStorage: AppStorage) { 

     setTimeout(() => { 
      console.log('Hello OauthService: appStorage=' + this.appStorage + ", http=" + this.http); 
     }, 3000) 
     //output - >Hello OauthService: appStorage=[object Object], http=[object Object] 
    } 
} 
+0

我需要说的是,你实际上并不需要为您提供OauthService静态参数吸气;它应该在没有吸气功能的情况下运行良好。 –

+0

嗨丈量,谢谢你的回答,一个疑问,如果我删除静态get参数块,appStorage工作正常,但HTTP是不确定变得构造oauthService里面.. – RHUL

+0

我删除了吸气功能,并指出,没有任何改变;两个服务对象都已定义。 –

相关问题