diff --git a/src/errorLog.js b/src/errorLog.js deleted file mode 100644 index 00b18b72..00000000 --- a/src/errorLog.js +++ /dev/null @@ -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) - }) - } -} diff --git a/src/main.js b/src/main.js index cb0ea88a..81082f84 100644 --- a/src/main.js +++ b/src/main.js @@ -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 diff --git a/src/settings.js b/src/settings.js index b6ffcc08..b566ef1d 100644 --- a/src/settings.js +++ b/src/settings.js @@ -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 diff --git a/src/utils/errorLog.js b/src/utils/errorLog.js new file mode 100644 index 00000000..7b33b542 --- /dev/null +++ b/src/utils/errorLog.js @@ -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) + }) + } +} diff --git a/src/utils/validate.js b/src/utils/validate.js index ada0e7e2..bd3f757b 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -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) +}