Skip to content

useCycleList

Category
Export Size
463 B
Last Changed
15 minutes ago

循环遍历一个项目列表。

通过 Vue School 的这个免费视频课程学习如何使用 useCycleList 创建图片轮播!

Demo

Dog

用法

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> {
  /**
   * 状态的初始值。
   * 可以提供一个 ref 来重用。
   */
  initialValue?: MaybeRef<T>
  /**
   * 当找不到索引时的默认索引。
   */
  fallbackIndex?: number
  /**
   * 获取当前值的索引的自定义函数。
   */
  getIndexOf?: (value: T, list: T[]) => number
}
/**
 * 循环浏览列表
 *
 * @see https://vueuse.org/useCycleList
 */
export declare function useCycleList<T>(
  list: MaybeRefOrGetter<T[]>,
  options?: UseCycleListOptions<T>,
): UseCycleListReturn<T>
export interface UseCycleListReturn<T> {
  state: Ref<T>
  index: Ref<number>
  next: (n?: number) => T
  prev: (n?: number) => T
  /**
   * Go to a specific index
   */
  go: (i: number) => T
}

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
Poet
xiaofan
Helio S. Junior
Waleed Khaled
Jelf
markthree
lsdsjy

变更日志

v10.8.0 on 2/20/2024
2ae36 - feat: add go function (#3615)
v10.1.0 on 4/22/2023
659b2 - fix: correctly wrap list with ref (#2988)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.0 on 3/14/2023
b65c2 - feat: allow receiving reactive list (#2864)

Released under the MIT License.