R2B*_*cks 2 internet-explorer typescript angular-cli angular5
我已经运行以下命令来禁用我的 angular 应用程序中生产环境的控制台日志。下面的代码在 chrome 上按预期工作,但是,它仍然在 IE 11 中显示日志。
主文件
if (environment.production) {
enableProdMode();
if(window){
window.console.log=function(){};
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个polyfill问题吗?我无法在网上找到任何关于它的信息。
编辑
这个问题可能看起来很相似,但没有解决我的问题,即为什么将控制台日志功能覆盖为空白方法在 chrome 中有效,但在 IE 11 中无效。
Mez*_*ane 21
问题已得到解答,答案已被接受,但我想向您展示一种在生产中禁用/关闭 console.log 的更好方法。在src/envirenmonts下添加一个包含以下内容的environment.ts :
export const environment = {
production: false,
mode: 'Dev'
}
Run Code Online (Sandbox Code Playgroud)
在main.ts中导入envirenmont const:
import './polyfills';
...
import { environment } from './environments/environment';
Run Code Online (Sandbox Code Playgroud)
现在添加以下代码片段:
..
if (environment.production) {
window.console.log = () => { }
}
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
...
}).catch(err => console.error(err));
Run Code Online (Sandbox Code Playgroud)
要尝试此操作,请在app.component.ts的构造函数中添加console.log:
...
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
...
constructor() {
console.log('How to switch off logging in Production?')
}
}
Run Code Online (Sandbox Code Playgroud)
将environment.product切换为true/false以查看结果这是一个正在运行的stackblitz
解决方案是将 polyfill 添加到您的 polyfill.ts 文件中
if(!window.console) {
var console = {
log : function(){},
warn : function(){},
error : function(){},
time : function(){},
timeEnd : function(){}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6819 次 |
| 最近记录: |