簡體   English   中英

有沒有辦法禁用控制台中出現的所有錯誤和警告?

[英]Is there any way to disable all the errors and warnings that appear in the console?

我不想在控制台中顯示某個開發環境的警告。 有沒有辦法實現這一目標? 我的應用程序是使用create react app引導的。

既然你正在使用React,我猜你已經在使用了babel。 有一個插件用於此目的。 它被稱為babel-plugin-transform-remove-console 這將在構建過程中排除所有console.log的語句。
在您的應用程序中安裝它並通過.babelrc配置,如下所示:

{
  "plugins": ["transform-remove-console"]
}

您還可以指定要排除的控制台功能的變體:

{
  "plugins": [ ["transform-remove-console", { "exclude": [ "error", "warn"] }] ]
}

我建議不要在代碼中使用控制台日志,除非必要。

在我的App.js中,我有以下代碼來完成此任務:

import { YellowBox } from 'react-native';

componentDidMount() {
    // The following lines are a workaround
    // in order to stop getting warnings about timer
    // See: https://github.com/firebase/firebase-js-sdk/issues/97#issuecomment-365456531
    YellowBox.ignoreWarnings(['Setting a timer']);
    const _console = _.clone(console);
    console.warn = message => {
      if (message.indexOf('Setting a timer') <= -1) {
        _console.warn(message);
      }
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM