Skip to content

useCycleList

类别
导出体积
444 B
上次更改
7 months ago

循环遍历一个项目列表。

通过 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'

类型声明

ts
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
:
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

更新日志

7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
2ae36 - feat: add go function (#3615)