Angular 模板 : Disable div in production mode

标签 angular typescript production-environment

我的 webapp 中有几个 div 仅用于调试,我想在生产模式下删除它们。

在我的模板中,我试图做这样的事情:

<div *ngIf=!environment.production>{{ fooState | async | json}}</div>

但是,在运行 ng serve --prod 时失败并出现以下错误:

ERROR in src/app/foo/foo.component.html(18,6): : Property 'environment' does not exist on type 'Foo'



有什么方法可以在生产模式下禁用这些,而无需为每个组件添加另一个字段?

我正在使用 Angular cli 1.7.4 和 Angular 5.2.10

最佳答案

如果 environment.production 为 false,您可以创建一个指令以仅显示您的 div 元素。类似于这个 blitzstack 示例:
https://stackblitz.com/edit/angular-eziobx

import { Directive, ViewContainerRef, OnInit, TemplateRef } from '@angular/core';
import { environment } from '../environment';

/**
 * usage: <div *hideProd></div>
 */
@Directive({
  selector: '[hideProd]'
})
export class HideProdDirective implements OnInit{

  constructor(private templateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef) { }

  ngOnInit(): void {
    if(!environment.prod){
      this.viewContainerRef.createEmbeddedView(this.templateRef);
    }
  }

}

关于 Angular 模板 : Disable div in production mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52612826/

相关文章:

angular - 依赖于其他效果结果的链式效果

c# - 创建 C# Winform 开发和生产环境

ruby-on-rails - 销毁路径在 Ruby on Rails 中不起作用

用于周 View 的 Angular 6+ 日历自定义事件模板不起作用

angular - 类不是 Angular 模块 - 仅在 IntelliJ/Webstorm 中导入本地构建 Angular 库时

angularjs - Sass 不使用 angular-cli 编译

html - Mat-table、mat-sort 无法正常工作

angular - 如何为 Angular react 形式动态设置禁用?

typescript - ESlint 不适用于带有 typescript 的 .vue 文件

django - gettext 翻译不适用于生产系统