feat(computed): add readonly flag if no setter is provided (#1654)
This commit is contained in:
parent
ad199e1a25
commit
dabdc5e115
@ -4,7 +4,8 @@ import {
|
|||||||
effect,
|
effect,
|
||||||
stop,
|
stop,
|
||||||
ref,
|
ref,
|
||||||
WritableComputedRef
|
WritableComputedRef,
|
||||||
|
isReadonly
|
||||||
} from '../src'
|
} from '../src'
|
||||||
import { mockWarn } from '@vue/shared'
|
import { mockWarn } from '@vue/shared'
|
||||||
|
|
||||||
@ -177,4 +178,22 @@ describe('reactivity/computed', () => {
|
|||||||
'Write operation failed: computed value is readonly'
|
'Write operation failed: computed value is readonly'
|
||||||
).toHaveBeenWarnedLast()
|
).toHaveBeenWarnedLast()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should be readonly', () => {
|
||||||
|
let a = { a: 1 }
|
||||||
|
const x = computed(() => a)
|
||||||
|
expect(isReadonly(x)).toBe(true)
|
||||||
|
expect(isReadonly(x.value)).toBe(false)
|
||||||
|
expect(isReadonly(x.value.a)).toBe(false)
|
||||||
|
const z = computed<typeof a>({
|
||||||
|
get() {
|
||||||
|
return a
|
||||||
|
},
|
||||||
|
set(v) {
|
||||||
|
a = v
|
||||||
|
}
|
||||||
|
})
|
||||||
|
expect(isReadonly(z)).toBe(false)
|
||||||
|
expect(isReadonly(z.value.a)).toBe(false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -2,6 +2,7 @@ import { effect, ReactiveEffect, trigger, track } from './effect'
|
|||||||
import { TriggerOpTypes, TrackOpTypes } from './operations'
|
import { TriggerOpTypes, TrackOpTypes } from './operations'
|
||||||
import { Ref } from './ref'
|
import { Ref } from './ref'
|
||||||
import { isFunction, NOOP } from '@vue/shared'
|
import { isFunction, NOOP } from '@vue/shared'
|
||||||
|
import { ReactiveFlags } from './reactive'
|
||||||
|
|
||||||
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
|
export interface ComputedRef<T = any> extends WritableComputedRef<T> {
|
||||||
readonly value: T
|
readonly value: T
|
||||||
@ -56,6 +57,9 @@ export function computed<T>(
|
|||||||
})
|
})
|
||||||
computed = {
|
computed = {
|
||||||
__v_isRef: true,
|
__v_isRef: true,
|
||||||
|
[ReactiveFlags.IS_READONLY]:
|
||||||
|
isFunction(getterOrOptions) || !getterOrOptions.set,
|
||||||
|
|
||||||
// expose effect so computed can be stopped
|
// expose effect so computed can be stopped
|
||||||
effect: runner,
|
effect: runner,
|
||||||
get value() {
|
get value() {
|
||||||
|
Loading…
Reference in New Issue
Block a user