董国庆
2026-06-04 1c43b80bd87ab2db74e32ac322fb6a4cc45d1d65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { router } from './router'
import App from './App.vue'
 
function getCurrentRouteInfo() {
  const currentRoute = router.currentRoute.value
  return {
    name: currentRoute?.name ?? '',
    path: currentRoute?.path ?? '',
    fullPath: currentRoute?.fullPath ?? '',
    query: currentRoute?.query ?? {},
    params: currentRoute?.params ?? {},
  }
}
 
function logGlobalError(type: string, payload: Record<string, unknown>) {
  console.error(`[global-error] ${type}`, {
    ...payload,
    route: getCurrentRouteInfo(),
    timestamp: new Date().toISOString(),
  })
}
 
window.addEventListener('error', (event) => {
  logGlobalError('window.error', {
    message: event.message,
    filename: event.filename,
    lineno: event.lineno,
    colno: event.colno,
    error: event.error,
  })
})
 
window.addEventListener('unhandledrejection', (event) => {
  logGlobalError('window.unhandledrejection', {
    reason: event.reason,
  })
})
 
// 创建应用实例并挂载
const app = createApp(App)
 
app.config.errorHandler = (error, instance, info) => {
  logGlobalError('vue.errorHandler', {
    info,
    component: instance?.type,
    error,
  })
}
 
app.mount('#app')