Skip to content

Commit 4555f7b

Browse files
committed
add errorLog option
1 parent 6637382 commit 4555f7b

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

src/errorLog.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import store from './store'
1515

1616
import i18n from './lang' // Internationalization
1717
import './icons' // icon
18-
import './errorLog' // error log
1918
import './permission' // permission control
2019
import './mock' // simulation data
20+
import './utils/errorLog' // error log
2121

2222
import * as filters from './filters' // global filters
2323

src/settings.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ export default {
2727
* @type {boolean} true | false
2828
* @description Need tagsView
2929
*/
30-
tagsView: true
30+
tagsView: true,
31+
32+
/**
33+
* @type {string | array} 'production' | ['production','development']
34+
* @description Need show err logs component.
35+
* The default is only used in the production env
36+
* If you want to use it in dev, you can pass ['production','development']
37+
*/
38+
errorLog: 'production'
3139

3240
// permission: true,
3341
// i18n: true

src/utils/errorLog.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Vue from 'vue'
2+
import store from '@/store'
3+
import { isString, isArray } from '@/utils/validate'
4+
import settings from '@/settings'
5+
6+
// you can set in settings.js
7+
// errorLog:'production' | ['production','development']
8+
const { errorLog: needErrorLog } = settings
9+
10+
function checkNeed(arg) {
11+
const env = process.env.NODE_ENV
12+
console.log(needErrorLog)
13+
if (isString(needErrorLog)) {
14+
return env === needErrorLog
15+
}
16+
if (isArray(needErrorLog)) {
17+
return needErrorLog.includes(env)
18+
}
19+
return false
20+
}
21+
22+
if (checkNeed()) {
23+
Vue.config.errorHandler = function(err, vm, info, a) {
24+
// Don't ask me why I use Vue.nextTick, it just a hack.
25+
// detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500
26+
Vue.nextTick(() => {
27+
store.dispatch('addErrorLog', {
28+
err,
29+
vm,
30+
info,
31+
url: window.location.href
32+
})
33+
console.error(err, info)
34+
})
35+
}
36+
}

src/utils/validate.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ export function validateEmail(email) {
4040
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,}))$/
4141
return re.test(email)
4242
}
43+
44+
export function isString(str) {
45+
if (typeof str === 'string' || str instanceof String) {
46+
return true
47+
}
48+
return false
49+
}
50+
51+
export function isArray(arg) {
52+
if (typeof Array.isArray === 'undefined') {
53+
return Object.prototype.toString.call(arg) === '[object Array]'
54+
}
55+
return Array.isArray(arg)
56+
}

0 commit comments

Comments
 (0)