无法绑定到 Directive('appHasAccess'),因为它不是“input”的已知属性

Par*_*iya 5 angularjs-directive angular angular-library angular5

我创建了一个自定义属性指令库包并将其安装到 myProject 中,当我尝试使用此自定义指令时会引发错误。

错误错误:未捕获(承诺):错误:模板解析错误:无法绑定到“appHasAccess”,因为它不是“输入”的已知属性。

我使用的代码如下:

所有可能的尝试我都做了。任何人都知道我如何解决这个问题。

1.指令:HasAccessDirective.ts

@Directive({
  selector: '[appHasAccess]',
})

export class HasAccessDirective {

accessDetail = { 'a': 1 }
 @Input('appHasAccess') set appHasAccess(accessDetail: any) {
    // Based on right control enable/disable
    this.eleRef.nativeElement.disabled = this.appRights.hasRights(accessDetail);
}
constructor(private eleRef: ElementRef,
    private appRights: MyService) { }
}
Run Code Online (Sandbox Code Playgroud)

2.模块:DigiUserRightsModule.ts

   @NgModule({
        declarations: [
            HasAccessDirective
        ],
        imports: [
            CommonModule,
            HttpClientModule,
        ],
        exports: [
            HasAccessDirective
        ],
        providers: [UserRightsService]
    })

export class DigiUserRightsModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: DigiUserRightsModule,
            providers: [UserRightsService]
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

Par*_*iya 3

我做了以下更改以使其正常工作。我将指令模块注入到我的用户模块而不是应用程序模块中。用户模块在其加载的路线上延迟加载并且工作正常。

  • 在用户模块设置我的指令包模块:

    import { NgModule }      from '@angular/core';       
    
    @NgModule({
       declarations: [],
       imports: [
           DigiUserRightsModule.forRoot()
       ],
       providers: []
    })
    
    export class UserModule {}
    
    Run Code Online (Sandbox Code Playgroud)