2017-03-05 84 views
1

我已抽象類收集,它利用服務數據庫來初始化數據庫上的集合。我有幾個子類的實例Collection,它們都需要做相同的數據庫初始化。所以集合是某種基類。抽象類未定義導入的服務

令人驚訝的進口服務數據庫是在子類(FooCollection)的構造不確定。在Foobar構造函數之前,肯定會調用數據庫服務的構造函數。

爲什麼導入的服務未定義在FooCollection?這是錯誤概念還是與類的加載順序有關的原因?

Collection.ts

import { Database } from '../providers/database'; 
import {Injectable} from "@angular/core"; 

@Injectable() 
export abstract class Collection { 

    public name : string; 

    constructor(public db : Database) { 
    // db is undefined here -> crash 
    db.doSomething(); 
    } 
} 

Database.ts

import {Injectable} from '@angular/core'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class Database { 

    private _db: any = null; 

    constructor() { 
    console.log('Hello Database Provider'); 
    } 

    public doSomething(): void { 
     console.log("doSomething"); 
    } 
} 

FooCollection.ts

import { Injectable } from '@angular/core'; 
import 'rxjs/add/operator/map'; 
import { Collection} from '../classes/collection'; 


@Injectable() 
export class FooCollection extends Collection{ 
} 

app.module.ts

import { NgModule, ErrorHandler } from '@angular/core'; 
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular'; 
import { MyApp } from './app.component'; 
import { Start } from '../pages/page/start'; 
import { Database } from '../providers/database'; 
import { FooCollection } from "../providers/foocollection"; 

@NgModule({ 
    declarations: [ 
    MyApp, 
    Start 
    ], 
    imports: [ 
    IonicModule.forRoot(MyApp) 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
    MyApp, 
    Start 
    ], 
    providers: [ 
    {provide: ErrorHandler, useClass: IonicErrorHandler}, 
    Database, 
    FooCollection 
    ] 
}) 
export class AppModule {} 

Start.ts

import { Component } from '@angular/core'; 

import { NavController } from 'ionic-angular'; 

import { FooCollection } from '../../providers/foocollection'; 

@Component({ 
    selector: 'page-start', 
    templateUrl: 'start.html' 
}) 
export class Start{ 
    constructor(public navCtrl: NavController, public f: FooCollection) { 
    } 
} 

編輯: 我注意到,明確對FooCollection再次導入Database正常工作:

import { Injectable } from '@angular/core'; 
import { Database } from './database'; 
import 'rxjs/add/operator/map'; 
import { Store } from '../classes/store'; 

@Injectable() 
export class Calls extends Store { 
    constructor(public db : Database) { 
    super(db); 
    } 
} 

這提高了e問題如何處理抽象類的子類上的導入。

+1

不知道這是否有幫助,但是你不需要在你的子類的(FooColleciton)構造函數中調用super()? – Aer0

+1

你使用哪個角度版本?您是否嘗試製作Collection非抽象類並直接將其用作提供者,例如{provide:FooCollection,useClass:Collection}?在哪裏以及如何使用FooCollection? – estus

+1

如果你在子類中添加構造函數,它是否工作? '@Injectable() export class FooCollection extends Collection { constructor(public db:Database){super(db); } }? –

回答

1

您應該在使用它的模塊中提供db。還應提供Foo收藏,因爲請參閱plunk

@NgModule({ 
    providers: [Database, FooCollection] 
+0

只有在模塊上:** AppModule **,添加app.module.ts定義以及 – kerosene

+1

你能實例化一個數據庫類嗎? –

+1

對集合的更多是抽象的,因此該類的實例屬於一個子類,它是一個可注入的實例,應該提供注入的數據庫實例。 –

0

我不能評論,所以我寫在這裏。 我剛剛將我的應用程序從2.4.5升級到4.0.0,現在我面臨同樣的問題! 我有一個抽象服務(a)注入緩存服務(作爲保護) 擴展該服務(b)添加功能。

和擴展服務(b)用於解析器。

解析器嘗試調用服務B中的方法,該方法使用A中的一個注入服務,但這些注入的服務未定義。 我再次檢查,運行在2.4。5 - 作品像一個魅力