Skip to content

useNProgress

Category
Export Size
425 B
Package
@vueuse/integrations
Last Changed
15 minutes ago

nprogress 的响应式封装。

Demo

Click to change progress status
Available in the @vueuse/integrations add-on.

安装

bash
npm i nprogress@^0

使用方法

js
import { useNProgress } from '@vueuse/integrations/useNProgress'

const { isLoading } = useNProgress()

function toggle() {
  isLoading.value = !isLoading.value
}

传递进度百分比

您可以传递一个百分比来指示进度条应该从哪里开始。

js
import { useNProgress } from '@vueuse/integrations/useNProgress'

const { progress } = useNProgress(0.5)

function done() {
  progress.value = 1.0
}

要更改进度百分比,请设置 progress.value = n,其中 n 是介于 0 到 1 之间的数字。

自定义

只需编辑 nprogress.css 以满足您的喜好。提示:您可能只想查找和替换 #29d 的出现。

您可以通过将对象作为第二个参数传递来配置它。

js
import { useNProgress } from '@vueuse/integrations/useNProgress'

useNProgress(null, {
  minimum: 0.1,
  // ...
})

类型声明

typescript
export type UseNProgressOptions = Partial<NProgressOptions>
/**
 * 响应式进度条。
 *
 * @see https://vueuse.org/useNProgress
 */
export declare function useNProgress(
  currentProgress?: MaybeRefOrGetter<number | null | undefined>,
  options?: UseNProgressOptions,
): {
  isLoading: WritableComputedRef<boolean, boolean>
  progress: Ref<
    number | (() => number | null | undefined) | null | undefined,
    | number
    | Ref<number | null | undefined, number | null | undefined>
    | ShallowRef<number | null | undefined>
    | WritableComputedRef<number | null | undefined, number | null | undefined>
    | ComputedRef<number | null | undefined>
    | (() => number | null | undefined)
    | null
    | undefined
  >
  start: () => nprogress.NProgress
  done: (force?: boolean) => nprogress.NProgress
  remove: () => void
}
export type UseNProgressReturn = ReturnType<typeof useNProgress>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Antério Vieira
Anthony Fu
Robert Soriano
Doctorwu
Agénor Debriat
webfansplz
Curt Grimes
Jelf
Michael Hüneburg
Alex Kozack

变更日志

v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter

Released under the MIT License.