跳到内容

useToNumber

分类
导出大小
223 B
上次更改
5 天前

响应式地将字符串 ref 转换为数字。

用法

ts
import { useToNumber } from '@vueuse/core'
import { shallowRef } from 'vue'

const str = shallowRef('123')
const number = useToNumber(str)

number.value // 123

类型声明

typescript
export interface UseToNumberOptions {
  /**
   * Method to use to convert the value to a number.
   *
   * Or a custom function for the conversion.
   *
   * @default 'parseFloat'
   */
  method?: "parseFloat" | "parseInt" | ((value: string | number) => number)
  /**
   * The base in mathematical numeral systems passed to `parseInt`.
   * Only works with `method: 'parseInt'`
   */
  radix?: number
  /**
   * Replace NaN with zero
   *
   * @default false
   */
  nanToZero?: boolean
}
/**
 * Reactively convert a string ref to number.
 */
export declare function useToNumber(
  value: MaybeRefOrGetter<number | string>,
  options?: UseToNumberOptions,
): ComputedRef<number>

源码

源码文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
btea
_Ghosteye

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生方法 (#4636)
v12.3.0 于 2025/1/2
635bb - feat: method 支持自定义函数 (#4441)
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,转而使用 Vue 的原生方法
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化包并清理 (#4349)
v10.0.0-beta.4 于 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
0a72b - feat(toValue): 将 resolveUnref 重命名为 toValue

在 MIT 许可证下发布。