Skip to content

computedInject

类别
导出体积
142 B
上次更改
6 minutes ago

结合 computedinject 使用。适用于基于注入的值创建计算属性。

示例

Array
[
  {
    "key": 1,
    "value": "1"
  },
  {
    "key": 2,
    "value": "2"
  },
  {
    "key": 3,
    "value": "3"
  }
]
Computed Array
[
  {
    "key": 0,
    "value": "all"
  },
  {
    "key": 1,
    "value": "1"
  },
  {
    "key": 2,
    "value": "2"
  },
  {
    "key": 3,
    "value": "3"
  }
]

用法

祖先组件

ts
import type { 
InjectionKey
,
Ref
} from 'vue'
import {
provide
,
ref
} from 'vue'
interface Item {
key
: number
value
: string
} export const
ArrayKey
:
InjectionKey
<
Ref
<Item[]>> =
Symbol
('symbol-key')
const
array
=
ref
([{
key
: 1,
value
: '1' }, {
key
: 2,
value
: '2' }, {
key
: 3,
value
: '3' }])
provide
(
ArrayKey
,
array
)
js
import { provide, ref } from 'vue'
export const ArrayKey = Symbol('symbol-key')
const array = ref([
  { key: 1, value: '1' },
  { key: 2, value: '2' },
  { key: 3, value: '3' },
])
provide(ArrayKey, array)

后代组件

ts
import { 
computedInject
} from '@vueuse/core'
import {
ArrayKey
} from './provider'
const
computedArray
=
computedInject
(
ArrayKey
, (
source
) => {
const
arr
= [...
source
.
value
]
arr
.
unshift
({
key
: 0,
value
: 'all' })
return
arr
})

默认值

你可以提供一个默认值,当父组件没有提供注入时使用该默认值。

ts
import { 
computedInject
} from '@vueuse/core'
const
computedArray
=
computedInject
(
ArrayKey, (
source
) => {
return
source
.
value
.
map
(
item
=>
item
.value)
},
ref
([]), // 默认的 source 值
)
js
import { computedInject } from '@vueuse/core'
const computedArray = computedInject(
  ArrayKey,
  (source) => {
    return source.value.map((item) => item.value)
  },
  ref([]),
)

工厂默认值

传入 true 作为第四个参数,将默认值视为工厂函数。

ts
import { 
computedInject
} from '@vueuse/core'
const
computedArray
=
computedInject
(
ArrayKey, (
source
) => {
return
source
.value.map(
item
=>
item
.value)
}, () =>
ref
([]), // 默认值的工厂函数
true, // 将默认值视为工厂函数 )
js
import { computedInject } from '@vueuse/core'
const computedArray = computedInject(
  ArrayKey,
  (source) => {
    return source.value.map((item) => item.value)
  },
  () => ref([]), // 默认值的工厂函数
  true,
)

可写的 computed

你也可以通过传入带有 getset 函数的对象,创建一个可写的计算属性。

ts
import { 
computedInject
} from '@vueuse/core'
const
computedArray
=
computedInject
(ArrayKey, {
get
(
source
) {
return
source
.value.map(
item
=>
item
.value)
},
set
(
value
) {
// 处理设置的值
console
.
log
('Setting value:',
value
)
}, })

类型声明

显示类型声明
ts
export type 
ComputedInjectGetter
<
T
,
K
> = (
source
:
T
| undefined,
oldValue
?:
K
,
) =>
K
export type
ComputedInjectGetterWithDefault
<
T
,
K
> = (
source
:
T
,
oldValue
?:
K
,
) =>
K
export type
ComputedInjectSetter
<
T
> = (
v
:
T
) => void
export interface
WritableComputedInjectOptions
<
T
,
K
> {
get
:
ComputedInjectGetter
<
T
,
K
>
set
:
ComputedInjectSetter
<
K
>
} export interface
WritableComputedInjectOptionsWithDefault
<
T
,
K
> {
get
:
ComputedInjectGetterWithDefault
<
T
,
K
>
set
:
ComputedInjectSetter
<
K
>
} export declare function
computedInject
<
T
,
K
= any>(
key
:
InjectionKey
<
T
> | string,
getter
:
ComputedInjectGetter
<
T
,
K
>,
):
ComputedRef
<
K
| undefined>
export declare function
computedInject
<
T
,
K
= any>(
key
:
InjectionKey
<
T
> | string,
options
:
WritableComputedInjectOptions
<
T
,
K
>,
):
ComputedRef
<
K
| undefined>
export declare function
computedInject
<
T
,
K
= any>(
key
:
InjectionKey
<
T
> | string,
getter
:
ComputedInjectGetterWithDefault
<
T
,
K
>,
defaultSource
:
T
,
treatDefaultAsFactory
?: false,
):
ComputedRef
<
K
>
export declare function
computedInject
<
T
,
K
= any>(
key
:
InjectionKey
<
T
> | string,
options
:
WritableComputedInjectOptionsWithDefault
<
T
,
K
>,
defaultSource
:
T
| (() =>
T
),
treatDefaultAsFactory
: true,
):
ComputedRef
<
K
>

源码

源码演示文档

贡献者

一纸忘忧

更新日志

没有最近的更新日志