2017-02-09 44 views
0

我有一個簡單的離子2應用程序。無法在離子2組件中注入服務

創建在提供服務

import { Storage } from '@ionic/storage'; 
import { Injectable } from '@angular/core'; 
import { Http, Headers } from '@angular/http'; 
import { AlertController } from 'ionic-angular'; 
import { NavController, NavParams } from 'ionic-angular'; 
import { AgendaPage } from '../pages/agenda/agenda'; 
import { LoginPage } from '../pages/login/login'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class Auth { 
    constructor(public http: Http, public storage: Storage, public alertCtrl: AlertController, public navCtrl: NavController) {} 
} 

app.components.ts登記

import { Component, ViewChild } from '@angular/core'; 
import { Nav, Platform } from 'ionic-angular'; 
import { StatusBar, Splashscreen } from 'ionic-native'; 

import { Auth } from '../providers/auth'; 
import { Rides } from '../providers/rides'; 

import { AgendaPage } from '../pages/agenda/agenda'; 
import { LoginPage } from '../pages/login/login'; 


@Component({ 
    templateUrl: 'app.html', 
    providers: [Auth, Rides] 
}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 

    rootPage: any = LoginPage; 

    pages: Array<{title: string, component: any}>; 

    constructor(public platform: Platform) { 
    this.initializeApp(); 

    // used for an example of ngFor and navigation 
    this.pages = [ 
     { title: 'Minha Agenda', component: AgendaPage } 
    ]; 

    } 

    initializeApp() { 
    this.platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 
    }); 
    } 

    openPage(page) { 
    // Reset the content nav to have just this page 
    // we wouldn't want the back button to show in this scenario 
    this.nav.setRoot(page.component); 
    } 
} 

嘗試將其注入到一個組件

import { Component } from '@angular/core'; 
import { NavController, NavParams } from 'ionic-angular'; 
import { Auth } from '../../providers/auth'; 

/* 
    Generated class for the Agenda page. 

    See http://ionicframework.com/docs/v2/components/#navigation for more info on 
    Ionic pages and navigation. 
*/ 
@Component({ 
    selector: 'page-agenda', 
    templateUrl: 'agenda.html', 
    providers: [Auth] 
}) 
export class AgendaPage { 

    openRides: any; 

    constructor(public navCtrl: NavController, public navParams: NavParams, private auth: Auth) {} 

} 

我得到以下錯誤:

Can't resolve all parameters for AgendaPage: (NavController, NavParams, ?).

我感到奇怪的是,我有一個非常相似的其他組件在那裏我可以用戶的服務沒有問題:

import { Component } from '@angular/core'; 
import { NavController, NavParams } from 'ionic-angular'; 
import { Auth } from '../../providers/auth'; 

/* 
    Generated class for the Login page. 

    See http://ionicframework.com/docs/v2/components/#navigation for more info on 
    Ionic pages and navigation. 
*/ 
@Component({ 
    selector: 'page-login', 
    templateUrl: 'login.html', 
    providers: [Auth] 
}) 
export class LoginPage { 

    email: string; 
    password: string; 

    constructor(public navCtrl: NavController, public navParams: NavParams, private auth: Auth) {} 

    login() { 

    } 

} 

這完美的作品。

+0

從構造函數中刪除參數。代碼中額外的參數是多餘的。 –

+0

@Ramon,哪個參數?你能舉個例子嗎? – almo

+0

不,它不起作用。我沒有爲你創建一個例子。繼續等待。 –

回答

1

如果是身份驗證服務,您可能不需要在較低的抽象級別提供它。它仍然需要導入才能使用,但不需要將它添加到較低層組件中的提供程序[]中。

這可能會導致錯誤,因爲某些原因,無論出於何種原因,它可能無法在該抽象級別提供Auth實例。值得注意的是,我儘量不要在我的服務上使用構造函數 - 這可能會導致問題(或者兩者都出現問題)。

+0

我將它從app.components.ts中的providers數組中提取出來,但保留在我的頁面組件中的providers數組中。錯誤消失了。但是現在我不能在其他服務中使用該服務了,因爲我沒有@Component裝飾器來設置我的提供者... – almo

+0

@almo嘗試做相反的事情,*僅*提供它在應用程序內。零件。 – chrispy

+0

我試過了,但它給了我同樣的錯誤:無法解析所有參數爲AgendaPage:(NavController,NavParams,Rides,?)..棘手... – almo

0

這裏的問題是循環依賴。您已經在您的服務中導入組件,並且希望將服務注入到相同的組件中。所以角度不知道首先加載哪個。 您需要查看forwardref。 在你的組件構造,

constructor(...,@Inject(forwardref(()=>Auth))auth) 

您可能要參考here更多。