跳到内容

useCycleList

类别
导出大小
454 B
最后更改
5 天前

循环遍历项目列表。

通过 Vue School 的免费视频课程,了解如何使用 useCycleList 创建图像轮播!

演示

用法

ts
import { useCycleList } from '@vueuse/core'

const { state, next, prev, go } = useCycleList([
  'Dog',
  'Cat',
  'Lizard',
  'Shark',
  'Whale',
  'Dolphin',
  'Octopus',
  'Seal',
])

console.log(state.value) // 'Dog'

prev()

console.log(state.value) // 'Seal'

go(3)

console.log(state.value) // 'Shark'

类型声明

typescript
export interface UseCycleListOptions<T> {
  /**
   * The initial value of the state.
   * A ref can be provided to reuse.
   */
  initialValue?: MaybeRef<T>
  /**
   * The default index when
   */
  fallbackIndex?: number
  /**
   * Custom function to get the index of the current value.
   */
  getIndexOf?: (value: T, list: T[]) => number
}
/**
 * Cycle through a list of items
 *
 * @see https://vueuse.org.cn/useCycleList
 */
export declare function useCycleList<T>(
  list: MaybeRefOrGetter<T[]>,
  options?: UseCycleListOptions<T>,
): UseCycleListReturn<T>
export interface UseCycleListReturn<T> {
  state: ShallowRef<T>
  index: WritableComputedRef<number>
  next: (n?: number) => T
  prev: (n?: number) => T
  /**
   * Go to a specific index
   */
  go: (i: number) => T
}

源码

源码演示文档

贡献者

Anthony Fu
IlyaL
Anthony Fu
Poet
xiaofan
Helio S. Junior
Waleed Khaled
Jelf
markthree
lsdsjy

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,支持 Vue 原生 (#4636)
v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,支持 Vue 原生
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化捆绑包并清理 (#4349)
v10.8.0 于 2024/2/20
2ae36 - feat: 添加 go 函数 (#3615)
v10.1.0 于 2023/4/22
659b2 - fix: 正确包装 ref 列表 (#2988)
v10.0.0-beta.4 于 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
10e98 - feat(toRef)!: 将 resolveRef 重命名为 toRef
0a72b - feat(toValue): 将 resolveUnref 重命名为 toValue
v10.0.0-beta.0 于 2023/3/14
b65c2 - feat: 允许接收响应式列表 (#2864)

根据 MIT 许可证发布。