Vue2源码分析-silent

257 阅读1分钟

silent

取消 Vue 所有的日志与警告。

  • 类型:boolean
  • 默认值:false
  • 用法:
 Vue.config.silent = true
  • 核心源码:
var config = ({
  // ...
  silentfalse,
  // ...
})
// ...
// 空函数,也叫占位函数
function noop (a, b, c) {}
var warn = noop;
var tip = noop;
var hasConsole = typeof console.log !== 'undefined'
// 警告
warn = function (msg, vm) {
  var trace = vm ? generateComponentTrace(vm) : '';
  // ...
  if (/*...*/) {
    // ...
  } else if (hasConsole && (!config.silent)) {
    console.error(("[Vue warn]: " + msg + trace));
  }
}
// 日志
tip = function (msg, vm) {
  if (hasConsole && (!config.silent)) {
    console.warn("[Vue tip]: " + msg + (
      vm ? generateComponentTrace(vm) : ''
    ))
  }
}

附录:github.com/fanqiewa/vu…