如何使ngIf指令在所有项目中全局可见? [英] How make ngIf directive globally visible in all project?

查看:133
本文介绍了如何使ngIf指令在所有项目中全局可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码<div class="flex-container" *ngIf="mail"> Some data </div>

我有一个错误Can't bind to 'ngIf' since it isn't a known property of 'div'.

我该如何解决?

推荐答案

在要使用通用指令的根模块中导入BrowserModule,在其他模块中导入CommonModule.

Import BrowserModule in the root module and CommonModule in other modules where you want to use common directives.

@NgModule({
  imports: [BrowserModule],
  ...
})
class AppModule {}

@NgModule({
  imports: [CommonModule],
  // Now MyComponent has access to ngIf
  declarations: [MyComponent]
  ...
})
class OtherModule {}

BrowserModule导出CommonModule,这样就不必直接在根模块中导入CommonModule.

BrowserModule exports CommonModule, this way it's not necessary to import CommonModule directly in the root module.

这篇关于如何使ngIf指令在所有项目中全局可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆