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

我刪除了吸氣功能,並指出,沒有任何改變;兩個服務對象都已定義。 –

相關問題