跳到主要内容

useCycleList

分类
导出大小
444 B
上次更改
8 个月前

循环遍历列表项。

通过 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
> {
/** * 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
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.8.0
2ae36 - feat: 添加 go 函数 (#3615)
v10.1.0
659b2 - fix: 正确地包装 ref 列表 (#2988)

根据 MIT 许可证发布。