跳到内容

reactiveOmit

类别
导出大小
403 B
上次更改
3 个月前

从响应式对象中响应式地省略字段。

用法

基本用法

ts
import { reactiveOmit } from '@vueuse/core'

const obj = reactive({
  x: 0,
  y: 0,
  elementX: 0,
  elementY: 0,
})

const picked = reactiveOmit(obj, 'x', 'elementX') // { y: number, elementY: number }

谓词用法

ts
import { reactiveOmit } from '@vueuse/core'

const obj = reactive({
  bar: 'bar',
  baz: 'should be omit',
  foo: 'foo2',
  qux: true,
})

const picked = reactiveOmit(obj, (value, key) => key === 'baz' || value === true)
// { bar: string, foo: string }

场景

选择性地将 props 传递给子组件

vue
<script setup>
import { reactiveOmit } from '@vueuse/core'

const props = defineProps({
  value: {
    default: 'value',
  },
  color: {
    type: String,
  },
  font: {
    type: String,
  }
})

const childProps = reactiveOmit(props, 'value')
</script>

<template>
  <div>
    <!-- only passes "color" and "font" props to child -->
    <ChildComp v-bind="childProps" />
  </div>
</template>

类型声明

typescript
export type ReactiveOmitPredicate<T> = (
  value: T[keyof T],
  key: keyof T,
) => boolean
export declare function reactiveOmit<T extends object, K extends keyof T>(
  obj: T,
  ...keys: (K | K[])[]
): Omit<T, K>
export declare function reactiveOmit<T extends object>(
  obj: T,
  predicate: ReactiveOmitPredicate<T>,
): Partial<T>

源码

源码文档

贡献者

Anthony Fu
Anthony Fu
Doctorwu
丶远方
Brain777777
qiang

更新日志

v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,推荐使用 Vue 原生方法
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化 bundles 并清理代码 (#4349)
v10.0.0-beta.4 于 2023/4/13
0a72b - feat(toValue): 将 resolveUnref 重命名为 toValue
v10.0.0-beta.2 于 2023/3/28
2e297 - feat: 添加谓词参数 (#2849)

在 MIT 许可证下发布。