add errorLog option
This commit is contained in:
		@@ -1,19 +0,0 @@
 | 
			
		||||
import Vue from 'vue'
 | 
			
		||||
import store from './store'
 | 
			
		||||
 | 
			
		||||
// you can set only in production env show the error-log
 | 
			
		||||
if (process.env.NODE_ENV === 'production') {
 | 
			
		||||
  Vue.config.errorHandler = function(err, vm, info, a) {
 | 
			
		||||
  // Don't ask me why I use Vue.nextTick, it just a hack.
 | 
			
		||||
  // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500
 | 
			
		||||
    Vue.nextTick(() => {
 | 
			
		||||
      store.dispatch('addErrorLog', {
 | 
			
		||||
        err,
 | 
			
		||||
        vm,
 | 
			
		||||
        info,
 | 
			
		||||
        url: window.location.href
 | 
			
		||||
      })
 | 
			
		||||
      console.error(err, info)
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -15,9 +15,9 @@ import store from './store'
 | 
			
		||||
 | 
			
		||||
import i18n from './lang' // Internationalization
 | 
			
		||||
import './icons' // icon
 | 
			
		||||
import './errorLog' // error log
 | 
			
		||||
import './permission' // permission control
 | 
			
		||||
import './mock' // simulation data
 | 
			
		||||
import './utils/errorLog' // error log
 | 
			
		||||
 | 
			
		||||
import * as filters from './filters' // global filters
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -27,7 +27,15 @@ export default {
 | 
			
		||||
   * @type {boolean} true | false
 | 
			
		||||
   * @description Need tagsView
 | 
			
		||||
   */
 | 
			
		||||
  tagsView: true
 | 
			
		||||
  tagsView: true,
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @type {string | array} 'production' | ['production','development']
 | 
			
		||||
   * @description Need show err logs component.
 | 
			
		||||
   * The default is only used in the production env
 | 
			
		||||
   * If you want to use it in dev, you can pass ['production','development']
 | 
			
		||||
   */
 | 
			
		||||
  errorLog: 'production'
 | 
			
		||||
 | 
			
		||||
  // permission: true,
 | 
			
		||||
  // i18n: true
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										36
									
								
								src/utils/errorLog.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/utils/errorLog.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
import Vue from 'vue'
 | 
			
		||||
import store from '@/store'
 | 
			
		||||
import { isString, isArray } from '@/utils/validate'
 | 
			
		||||
import settings from '@/settings'
 | 
			
		||||
 | 
			
		||||
// you can set in settings.js
 | 
			
		||||
// errorLog:'production' | ['production','development']
 | 
			
		||||
const { errorLog: needErrorLog } = settings
 | 
			
		||||
 | 
			
		||||
function checkNeed(arg) {
 | 
			
		||||
  const env = process.env.NODE_ENV
 | 
			
		||||
  console.log(needErrorLog)
 | 
			
		||||
  if (isString(needErrorLog)) {
 | 
			
		||||
    return env === needErrorLog
 | 
			
		||||
  }
 | 
			
		||||
  if (isArray(needErrorLog)) {
 | 
			
		||||
    return needErrorLog.includes(env)
 | 
			
		||||
  }
 | 
			
		||||
  return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if (checkNeed()) {
 | 
			
		||||
  Vue.config.errorHandler = function(err, vm, info, a) {
 | 
			
		||||
  // Don't ask me why I use Vue.nextTick, it just a hack.
 | 
			
		||||
  // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500
 | 
			
		||||
    Vue.nextTick(() => {
 | 
			
		||||
      store.dispatch('addErrorLog', {
 | 
			
		||||
        err,
 | 
			
		||||
        vm,
 | 
			
		||||
        info,
 | 
			
		||||
        url: window.location.href
 | 
			
		||||
      })
 | 
			
		||||
      console.error(err, info)
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -40,3 +40,17 @@ export function validateEmail(email) {
 | 
			
		||||
  const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
 | 
			
		||||
  return re.test(email)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function isString(str) {
 | 
			
		||||
  if (typeof str === 'string' || str instanceof String) {
 | 
			
		||||
    return true
 | 
			
		||||
  }
 | 
			
		||||
  return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function isArray(arg) {
 | 
			
		||||
  if (typeof Array.isArray === 'undefined') {
 | 
			
		||||
    return Object.prototype.toString.call(arg) === '[object Array]'
 | 
			
		||||
  }
 | 
			
		||||
  return Array.isArray(arg)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user