跳到主要內容

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 }

Predicate 用法

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): 棄用 toValue,改用 Vue 原生方法,原 @vueuse/shared 中的 toValue 將被棄用
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: 新增 predicate 參數 (#2849)

根據 MIT 授權條款發布。