跳到主要内容

useToNumber

分类
导出大小
213 B
上次更改
3 个月前

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

用法

ts
import { 
useToNumber
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
str
=
shallowRef
('123')
const
number
=
useToNumber
(
str
)
number
.
value
// 123

类型声明

ts
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. * * @__NO_SIDE_EFFECTS__ */ export declare function
useToNumber
(
value
:
MaybeRefOrGetter
<number | string>,
options
?: UseToNumberOptions,
):
ComputedRef
<number>

来源

源代码文档

贡献者

Anthony Fu
Anthony Fu
IlyaL
SerKo
btea
_Ghosteye

更新日志

v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
635bb - feat: method 支持自定义函数 (#4441)
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)

根据 MIT 许可证发布。