yanjinhui
2025-07-29 c5de0d98241f8c8349fa38851b77efcfc61e4d26
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
import { nextTick, unref } from 'vue'
import type { NProgressOptions } from 'nprogress'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { useCssVar } from '@vueuse/core'
 
const primaryColor = useCssVar('--el-color-primary', document.documentElement)
 
export const useNProgress = () => {
  NProgress.configure({ showSpinner: false } as NProgressOptions)
 
  const initColor = async () => {
    await nextTick()
    const bar = document.getElementById('nprogress')?.getElementsByClassName('bar')[0] as ElRef
    if (bar) {
      bar.style.background = unref(primaryColor.value) as string
    }
  }
 
  initColor()
 
  const start = () => {
    NProgress.start()
  }
 
  const done = () => {
    NProgress.done()
  }
 
  return {
    start,
    done
  }
}