2017-01-16 91 views
8

我正在尝试使用angular-cli来获得AOT编译设置。我有一个从抽象类继承的指令,并且在编译期间出现错误,无法确定抽象类属于哪个模块。我不能将它添加到NgModule的声明数组中,那么有什么正确的方法可以解决这个问题呢?我的代码结构看起来像这样,Angular 2 angular-cli AOT在模块中声明抽象类?

//...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. 
} 

错误看起来像这样

ERROR in Cannot determine the module for class TutorialDirective in /test-app/src/app/tutorial/directive/tutorial.directive.ts! 

我的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 { } 

好一些调试后,如果我做它不是抽象的,它添加到声明它的作品。这是否意味着我不能将课程标记为抽象?这似乎不正确的...

+0

为您的模块添加代码app.module.ts – Aravind

+0

@Aravind ok。我无法将TutorialDirective添加到声明数组中。只是让你知道。 – Steveadoo

+0

好吧,如果我不把它抽象并将它添加到声明它的工作。这是否意味着我不能将课程标记为抽象?这似乎不正确... – Steveadoo

回答

0

是..这是真的,如果你创建一个抽象类,并尝试使用它作为指令的控制器,它不会工作的原因是在此间举行的documentation

以下文档的重点部分如下enter image description here

当您创建指令时,Angular将创建指令的控制器类的新实例。当你使用抽象类时,你不能拥有它的实例,因此它在你的情况下失败

+4

我有一个实现类,它只是从实例角度的基类继承而来创造,而不是抽象的。如果我使用JIT,但不是AOT,这一切都有效。我不确定就是这样。 – Steveadoo

+1

同样的问题在这里,你有任何解决方法? – godzsa

2

从抽象类中删除@Component装饰器。

+0

这对我来说似乎是正确的解决方案。你不应该需要装饰器,因为每个实现抽象类的类都会有自己的装饰器。一旦为抽象类删除了装饰器,您就不需要将它放入ngModule声明中,因为它现在只是一个正常的打字稿导入。 – apricity

+0

我还必须添加@Injectable()来代替@Component才能正常工作 – apricity

+0

IMO此解决方案最有意义;因为它是抽象的,所以它不能真正被认为是什么角度定义为一个组件。 –