Angular 2 angular-cli AOT在模块中声明抽象类?

Ste*_*doo 18 angular-cli angular

我正在尝试使用angular-cli进行AOT编译设置.我有一个从抽象类继承的指令,我在编译期间遇到一个错误,即angular无法确定抽象类属于哪个模块.我无法将它添加到NgModule的声明数组中,那么正确的方法是什么呢?我的代码结构如下所示,

//...imports
export abstract class TutorialDirective  {
    //...base class logic
}

@Directive({
    selector: '[tut]',
    exportAs: 'tut'
})
export class DefaultTutorialDirective extends TutorialDirective {
    //...calls into the base class for some shared stuff.
}
Run Code Online (Sandbox Code Playgroud)

错误看起来像这样

ERROR in Cannot determine the module for class TutorialDirective in /test-app/src/app/tutorial/directive/tutorial.directive.ts!
Run Code Online (Sandbox Code Playgroud)

我的AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

import { TutorialService } from './tutorial/tutorial.service';
import { TutorialDirective, DefaultTutorialDirective } from './tutorial/directive/tutorial.directive';

@NgModule({
  declarations: [
    AppComponent,
    DefaultTutorialDirective
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [TutorialService],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

好的经过一些调试,如果我把它变成抽象并将其添加到声明中就可以了.这是否意味着我不能将一个类标记为抽象?这似乎不对......

Vel*_*ter 10

从抽象类中删除@Component装饰器,将其替换为@Injectable()装饰器.


Ara*_*ind 0

是的..确实,如果您创建一个抽象类并尝试将其用作指令控制器,它将不起作用,原因在文档

文档的突出显示部分如下在此输入图像描述

当你创建一个指令时,Angular 就会创建该指令的控制器类的一个新实例。当您使用抽象类时,您无法拥有它的实例,因此在您的情况下它会失败

  • 我有一个实现类,它只是从基类继承,基类是 Angular 将创建的实例,而不是抽象类。如果我使用 JIT 而不是 AOT,这一切都有效。我不确定就是这样。 (4认同)