【问题标题】:Change console.log() for production deployments?更改console.log() 以进行生产部署?
【发布时间】:2019-08-31 11:45:33
【问题描述】:

我通过大量使用console.log() 输出了很多内部信息。有没有办法在 Angular 中为我的 --prod 构建更改 .log() 命令?

【问题讨论】:

  • 您可以使用代码编辑器中的查找和替换功能来注释所有不必要的日志
  • 如果你使用过tslint,你可以设置no-console规则,它会提醒你删除过多的console.logs
  • remove console.logs with Webpack & Uglify 的可能重复项。简而言之:使用 TerserPlugin 并将 drop_console 选项设置为 true

标签: angular


【解决方案1】:

根据 Yash Rami 的回答,我发现我需要在 main.ts 中使用 window.console.log()

if(environment.production){
    window.console.log = function() {}
    enableProdMode();
}

【讨论】:

    【解决方案2】:

    您需要在 main.ts 中编写此内容。希望对你有帮助

          import { enableProdMode } from '@angular/core';
          import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
          import { AppModule } from './app/app.module';
          import { environment } from './environments/environment';
    
          if (environment.production) {
            console.log = function() {}   // this will be disable all the logs in production mode 
            enableProdMode();
          }
    
          platformBrowserDynamic().bootstrapModule(AppModule)
          .catch(err => console.log(err));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2023-03-31
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      相关资源
      最近更新 更多