useCycleList
循環瀏覽項目列表。
透過 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
}
來源
貢獻者
更新日誌
v12.8.0
於 2025/3/5v12.3.0
於 2025/1/259f75
- feat(toValue):棄用 @vueuse/shared
中的 toValue
,改用 Vue 原生功能v12.0.0-beta.1
於 2024/11/21v10.8.0
於 2024/2/20v10.1.0
於 2023/4/22v10.0.0-beta.4
於 2023/4/134d757
- feat(types)!:將 MaybeComputedRef
重新命名為 MaybeRefOrGetter
10e98
- feat(toRef)!:將 resolveRef
重新命名為 toRef
0a72b
- feat(toValue):將 resolveUnref
重新命名為 toValue
v10.0.0-beta.0
於 2023/3/14