angular - 导入的服务在抽象类上未定义

标签 angular typescript dependency-injection ionic2

我有抽象类 Collection,它利用服务 Database 来初始化数据库上的集合。我有几个 Collection 的子类实例,它们都需要进行相同的数据库初始化。所以 Collection 是某种基类。

令人惊讶的是,导入的服务 Database 在子类 FooCollection 的构造函数中是 undefinedDatabase 服务的构造函数肯定在 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();
  }
}

数据库.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);
  }
}

这提出了一个问题,即如何在抽象类的子类上处理导入。

最佳答案

您应该在使用它的模块中提供数据库。还应提供 Foo 集合,因为请参阅 plunk .

@NgModule({
  providers: [Database, FooCollection]

关于angular - 导入的服务在抽象类上未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42614887/

相关文章:

typescript pdfjs-dist : You may need an additional loader

javascript - TypeScript 可以为 Closure Compiler 输出注释吗?

.net - 插件架构/依赖注入(inject)问题

javascript - 在表格中显示 Angular 中单个对象的数据时出现问题

node.js - Stripe Angular 错误: Uncaught (in promise): TypeError: Cannot read property 'stripeService' of undefined

javascript - Angular Firestore 查询中的 get() 和 valueChanges() 有什么区别?

c# - 如何配置 Ninject 以便它根据先前注入(inject)的实例注入(inject)正确的实例

json - Angular2 从 JSON 解析为对象

javascript - Angular 2 : nested *ngFor + md-select

c# - 具有多个表的存储库模式