跳到主要內容

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.dev.org.tw/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 支援,最佳化 bundle 並清理 (#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 授權條款發布。