自定义Angular 2指令不工作

自定义Angular 2指令不工作,angular,angular2-directives,Angular,Angular2 Directives,错误 模板分析错误: 无法绑定到“时间增量”,因为它不是“p”的已知属性 文档中的解决方案 我必须将该指令添加到声明数组中。我已经做过了。 现在,我的架构: 我有三个模块: AppModule(根目录) TimeModule(稍后应该是带有一些指令的帮助器模块) AuthModule(登录、注册组件等) AppModule: @NgModule({ imports: [ TimeModule, BrowserModule, FormsM

错误
模板分析错误:
无法绑定到“时间增量”,因为它不是“p”的已知属性

文档中的解决方案
我必须将该指令添加到声明数组中。我已经做过了。

现在,我的架构: 我有三个模块:

  • AppModule(根目录)
  • TimeModule(稍后应该是带有一些指令的帮助器模块)
  • AuthModule(登录、注册组件等)
AppModule:

@NgModule({
    imports: [
        TimeModule,
        BrowserModule,
        FormsModule,
        AuthModule,
        routing,
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        appRoutingProviders
    ],
    bootstrap: [AppComponent]
})
授权模块:

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        authRouting
    ],
    declarations: [
        AuthComponent,
        LoginComponent,
        SignupComponent,
        LogoutComponent
    ],
    providers: [
        AuthGuard,
        AuthService
    ],
    bootstrap: [ LoginComponent ]
})
时间模块:

@NgModule({
    declarations: [
        ReadableDatePipe,
        TimeDeltaDirective
    ]
})
现在我正试图在LoginComponent视图中使用TimeDeltaDirect。 我已经尝试将该指令添加到其他声明数组中,但这也不起作用

我感谢所有的支持!谢谢

TimeDeltaDirective:

import { Directive, ElementRef, Input, Renderer } from '@angular/core';

@Directive({ selector: '[time-delta]' })
export class TimeDeltaDirective {
    constructor(renderer: Renderer, el: ElementRef) {
        function getTimeDelta(date: Date) {
            var now = new Date();
            return (now.getTime() - date.getTime()) / 1000;
        }

        this.delta = getTimeDelta(new Date(this.date));
    }

    @Input('date') date: string;
    delta: number;
}
LoginComponent中的用法(示例):

@组件({
选择器:“登录”,
模板:`

{{{delta}

` })
您需要从
TimeModule
导出
TimeDeltaDirective
,然后在
AuthModule
中导入
TimeModule
,因为您的
LoginComponent
在那里贴有标签,这就是您想要使用指令的地方。这样,
TimeDeltaDirective
将可用于
LoginComponent
,以及
AuthModule
的其他声明组件。所以,应该是这样的:

AuthModule

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        authRouting,
        TimeModule
    ],
    declarations: [
        AuthComponent,
        LoginComponent,
        SignupComponent,
        LogoutComponent
    ],
    providers: [
        AuthGuard,
        AuthService
    ],
    bootstrap: [ LoginComponent ]
})
@NgModule({
    declarations: [
        ReadableDatePipe,
        TimeDeltaDirective
    ],
    exports: [
        TimeDeltaDirective
    ]
})
时间模块

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        authRouting,
        TimeModule
    ],
    declarations: [
        AuthComponent,
        LoginComponent,
        SignupComponent,
        LogoutComponent
    ],
    providers: [
        AuthGuard,
        AuthService
    ],
    bootstrap: [ LoginComponent ]
})
@NgModule({
    declarations: [
        ReadableDatePipe,
        TimeDeltaDirective
    ],
    exports: [
        TimeDeltaDirective
    ]
})

谢谢你的回答。不幸的是,这不起作用。我将
TimeDeltaDirective
添加到导出数组中,并将其添加到
AuthModule
的导入数组中。还是一样的错误。还有其他想法吗?@UeliKramer您是否将
TimeDeltaDirective
TimeModule
添加到
AuthModule
导入中?我已将
TimeModule
添加到导入中。@UeliKramer那么您的导入/导出都很好,可能您的指令有问题,您应该在这里发布。您有什么建议吗?