【问题标题】:Angular 8 Directives from shared module is not working来自共享模块的 Angular 8 指令不起作用
【发布时间】:2020-04-24 11:23:33
【问题描述】:

大家好!我正在尝试制作一个 Angular 8 自定义指令,但它对我不起作用,浏览器控制台没有显示任何错误,但我没有可视化我留在代码中的更改或 console.logs,它是如果该指令从未被调用。请有人帮助我!非常感谢。我有这个,我做错了什么?

// has-permission.directive.ts

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

@Directive({
  selector: '[permission]'
})

export class HasPermissionDirective {

  constructor(private el: ElementRef) { }

  @Input() permission: string;

  OnInit() {
    console.log('this.permission->', this.permission)
    console.log('text', this.el.nativeElement.textContent += 'It is working');
    console.log('--------------------------------------------------');
  }

}
// shared.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HasPermissionDirective } from '../directives/has-permission.directive';  //<-- My directive

@NgModule({
  declarations: [HasPermissionDirective],  //<-- Declaring
  imports: [ ],
  exports: [
    HasPermissionDirective,  //<-- exporting
    CommonModule,
  ]
})
export class SharedModule { }
//dashboards.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ComponentsModule } from '../../components/components.module';

import { BsDropdownModule } from 'ngx-bootstrap';
import { ProgressbarModule } from 'ngx-bootstrap/progressbar';
import { TooltipModule } from 'ngx-bootstrap/tooltip';

import { DashboardComponent } from './dashboard/dashboard.component';
import { AlternativeComponent } from './alternative/alternative.component';

import { RouterModule } from '@angular/router';
import { DashboardsRoutes } from './dashboards.routing';
import { SharedModule } from '../../shared/shared.module'; //<-- Here

@NgModule({
  declarations: [DashboardComponent, AlternativeComponent],
  imports: [
    CommonModule,
    ComponentsModule,
    BsDropdownModule.forRoot(),
    ProgressbarModule.forRoot(),
    TooltipModule.forRoot(),
    RouterModule.forChild(DashboardsRoutes),
    SharedModule,  //<-- Here
  ],
  exports: [DashboardComponent, AlternativeComponent]
})
export class DashboardsModule {}

// dashboard.component.html
<h6 [permission]="permission" class="h2 text-white d-inline-block mb-0" >Default</h6>

请帮帮我!! T_T

【问题讨论】:

  • 我看到了一个“奇怪的东西”。 1.- 您在 sharedModule 中导出“CommonModule”-我认为您需要删除它,2.- 您是否从“@angular/platform-b​​rowser”导入 {BrowserModule};在您的主模块中-不在问题中-?
  • 是的,你也可以,我会从那里删除它。非常感谢!
  • 您的帖子比在模块内共享指令的角度文档要好,谢谢!

标签: angular typescript angular2-directives


【解决方案1】:

你的类应该实现 OnInit 生命周期钩子(不是强制的,但是一个好的做法)并且方法应该是ngOnInit()

export class HasPermissionDirective implements OnInit {

  constructor(private el: ElementRef) { }

  ngOnInit() {
    console.log('--------------------------------------------------');
  }

}

在 Eliseo 提到的共享模块中,您应该只导入公共模块而不是导出它。

【讨论】:

  • 没错。我对 OnInit 生命周期的实现很糟糕......你是最好的! 非常感谢!!
  • 不客气。如果它解决了您的问题,请不要忘记将答案标记为正确。编码愉快!
  • 我怀疑这是否有助于克服上述问题。
  • 就我而言,我没有从 SharedModule 导出指令。因此,为我从共享的已修复问题中导出指令。
  • 就我而言,我正在实施生命周期,但名称错误,我使用的是 OnInit insted of ngOnInit @CharithaGoonewardena ?
猜你喜欢
  • 2017-03-17
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 2019-11-05
  • 2017-02-07
  • 2014-06-22
相关资源
最近更新 更多