vueuse-math-skilld
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesevueuse/vueuse @vueuse/math
@vueuse/mathvueuse/vueuse @vueuse/math
@vueuse/mathVersion: 14.2.1 (Feb 2026)
Deps: @vueuse/shared@14.2.1
Tags: alpha: 14.0.0-alpha.3 (Sep 2025), beta: 14.0.0-beta.1 (Sep 2025), latest: 14.2.1 (Feb 2026)
References: Docs — API reference, guides • GitHub Issues — bugs, workarounds, edge cases • GitHub Discussions — Q&A, patterns, recipes • Releases — changelog, breaking changes, new APIs
**版本:**14.2.1(2026年2月)
依赖:@vueuse/shared@14.2.1
**标签:**alpha: 14.0.0-alpha.3(2025年9月),beta: 14.0.0-beta.1(2025年9月),latest: 14.2.1(2026年2月)
参考链接:文档 — API参考、使用指南 • GitHub问题 — 问题反馈、解决方案、边缘场景 • GitHub讨论 — 问答、模式分享、实践方案 • 版本发布记录 — 更新日志、破坏性变更、新API
API Changes
API变更
This section documents version-specific API changes — prioritize recent major/minor releases.
-
DEPRECATED:,
and,or— v14 deprecated in favor of original namesnot,logicAnd,logicOrsourcelogicNot -
BREAKING: Requires Vue 3.5+ — v14 moved to Vue 3.5 as minimum version, enabling nativeand
useTemplateRefsourceMaybeRefOrGetter -
BREAKING: ESM-only — v13 dropped CommonJS (CJS) support entirely source
-
NEW:— reactively calculate average from an array or variadic arguments
useAverage -
NEW:— reactively calculate sum from an array or variadic arguments
useSum -
NEW:— create a reusable numeric projector between two numeric domains
createProjection -
NEW:— create a projector with a custom mapping function for arbitrary types
createGenericProjection -
NEW:— options now include
usePrecisionproperty for rounding strategy (math,floor,ceil)round -
NEW:— returns
useClampinstead ofComputedRefwhen input is a getter or readonly refRef -
NEW:— provides reactive access to any standard
useMathmethod via key nameMath -
NEW:,
logicAnd,logicOr— variadic reactive boolean logic supporting multiple refslogicNot -
NEW:,
useMax— support both array and variadic arguments for reactive comparisonuseMin -
NEW:,
useAbs,useCeil,useFloor,useRound— dedicated reactive wrappers for common Math methodsuseTrunc -
NEW:— reactive numeric projection from one domain to another
useProjection
Also changed: build system v14 · types v14 · native v12.8
tsdownWatchSource<T>MaybeRefOrGetter本节记录各版本的API变更信息 — 优先关注近期的主版本/次版本更新。
-
已废弃:、
and、or— v14版本中已废弃,推荐使用原名not、logicAnd、logicOr来源logicNot -
破坏性变更:要求Vue 3.5+ — v14版本将最低支持版本升级为Vue 3.5,以支持原生和
useTemplateRef来源MaybeRefOrGetter -
破坏性变更:仅支持ESM — v13版本完全移除了CommonJS(CJS)支持 来源
-
新增:— 响应式计算数组或多个参数的平均值
useAverage -
新增:— 响应式计算数组或多个参数的总和
useSum -
新增:— 创建可复用的数值投影器,实现两个数值域之间的映射
createProjection -
新增:— 创建支持自定义映射函数的投影器,适用于任意类型的域映射
createGenericProjection -
新增:— 配置项新增
usePrecision属性,用于指定舍入策略(math、floor、ceil)round -
新增:— 当输入为getter或只读ref时,返回
useClamp而非ComputedRefRef -
新增:— 通过键名提供对标准
useMath方法的响应式访问Math -
新增:、
logicAnd、logicOr— 支持多个ref的可变参数响应式布尔逻辑运算logicNot -
新增:、
useMax— 同时支持数组和可变参数,实现响应式比较useMin -
新增:、
useAbs、useCeil、useFloor、useRound— 为常用Math方法提供专用的响应式包装器useTrunc -
新增:— 实现从一个数值域到另一个数值域的响应式数值投影
useProjection
**其他变更:**v14版本的构建系统 · v14版本的类型 · v12.8版本原生支持
tsdownWatchSource<T>MaybeRefOrGetterBest Practices
最佳实践
- Use with a mutable
useClampto create a self-validating state. When a mutable ref is passed, it returns a writable computed that automatically clamps any value assigned to it sourceref
ts
// Preferred: prevents invalid state assignment
const value = useClamp(shallowRef(0), 0, 10)
value.value = 15 // state remains 10-
Pass reactive arrays for domains into handle dynamic scaling. This is preferred for UI elements like zoomable charts or responsive sliders where the input/output boundaries change over time source
useProjection -
Define reusable mappers withoutside component logic. This ensures consistent scaling across different parts of the application and reduces the overhead of redefining domains source
createProjection -
Leverage rest arguments in aggregation composables for ad-hoc calculations.,
useSum,useAverage, anduseMaxaccept multiple refs directly, avoiding the need to create intermediate array refsuseMin
ts
// Preferred: cleaner syntax for fixed sets of refs
const total = useSum(refA, refB, refC)-
Preferover
usePrecisionfor numeric operations.toFixedreturns ausePrecision, which prevents type-coercion bugs and allows further mathematical operations without re-parsing strings sourcenumber -
Use explicit rounding modes infor specific UI requirements. Pass the
usePrecisionoption ('floor' | 'ceil' | 'round') to control how fractional values are handled in paginators or progress bars sourcemath -
Combineor
logicAndwithlogicOr's@vueuse/corefor cleaner side effects. This pattern is more readable than complex manualwheneverproperties when triggering actions based on multiple reactive flags sourcecomputed -
Employfor non-linear domain mapping. Provide a custom projector function to handle logarithmic scales or custom eased transitions between numeric domains source
createGenericProjection -
Useto reactively derive values from standard
useMathmethods. It automatically wraps multiple arguments and ensures the result updates whenever any input dependency changes sourceMath -
Usefor reactive boolean inversion in templates. It expresses intent more clearly than
logicNotor manual!ref.valuewrappers when defining visibility or disabled statescomputed
- 将与可变
useClamp配合使用,创建自验证状态。当传入可变ref时,它会返回一个可写的computed对象,自动限制分配给它的任何值 来源ref
ts
// 推荐写法:防止无效状态赋值
const value = useClamp(shallowRef(0), 0, 10)
value.value = 15 // 状态仍保持为10-
在中传入响应式数组作为域,以处理动态缩放。这适用于可缩放图表或响应式滑块等UI元素,这类场景中输入/输出边界会随时间变化 来源
useProjection -
在组件逻辑外部使用定义可复用的映射器。这能确保应用不同部分的缩放逻辑一致,同时减少重复定义域的开销 来源
createProjection -
在聚合类组合式函数中使用剩余参数进行临时计算。、
useSum、useAverage和useMax直接接受多个ref,无需创建中间数组refuseMin
ts
// 推荐写法:固定数量ref的更简洁语法
const total = useSum(refA, refB, refC)-
数值运算优先使用而非
usePrecision。toFixed返回usePrecision类型,可避免类型转换错误,且无需重新解析字符串即可进行后续数学运算 来源number -
针对特定UI需求,在中使用显式的舍入模式。传入
usePrecision选项('floor' | 'ceil' | 'round')来控制分页器或进度条等组件中分数值的处理方式 来源math -
将或
logicAnd与@vueuse/core的logicOr配合使用,实现更简洁的副作用逻辑。当基于多个响应式标志触发操作时,这种模式比复杂的手动whenever属性更具可读性 来源computed -
使用实现非线性域映射。提供自定义投影函数来处理对数缩放或数值域之间的自定义缓动过渡 来源
createGenericProjection -
使用从标准Math方法中响应式推导值。它会自动包装多个参数,并确保当任何输入依赖项变化时结果都会更新 来源
useMath -
在模板中使用进行响应式布尔值取反。在定义可见性或禁用状态时,它比
logicNot或手动!ref.value包装器更能清晰表达意图computed