add errorLog option

This commit is contained in:
Pan 2018-10-10 15:47:36 +08:00
parent 663738262d
commit 4555f7b91b
5 changed files with 60 additions and 21 deletions

View File

@ -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)
})
}
}

View File

@ -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

View File

@ -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
View 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)
})
}
}

View File

@ -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)
}