在生产中禁用 console.log() [英] Disabling console.log() in production

查看:30
本文介绍了在生产中禁用 console.log()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已运行以下命令以在我的 Angular 应用程序中禁用生产环境的控制台日志.下面的代码在 chrome 上按预期工作,但是,它仍然在 IE 11 中显示日志.

I have run the following to disable console logs for production environments in my angular application. The below code works as expected for chrome, however, it still shows logs in IE 11.

main.ts

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

这是一个 polyfill 问题吗?我在网上找不到任何关于它的信息.

Is this a polyfill issue? I wasn't able to find anything online about it.

编辑

这个问题 可能看起来很相似,但没有解决我的问题,即为什么将控制台日志功能覆盖为空白方法在 chrome 中有效,但在 IE 11 中无效.

This question may seem similar but does not address my issue as to why overriding the console log function to a blank method works in chrome but not IE 11.

推荐答案

解决方案是将 polyfill 添加到您的 polyfill.ts 文件中

Solution is to add the polyfill to your polyfill.ts file

if(!window.console) {
 var console = {
  log : function(){},
  warn : function(){},
  error : function(){},
  time : function(){},
  timeEnd : function(){}
 }
}

这篇关于在生产中禁用 console.log()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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