perf(runtime-core): use raw context on component options init

This commit is contained in:
Evan You 2020-04-16 10:39:51 -04:00
parent 24e5ab33f5
commit bfd6744fb1
2 changed files with 9 additions and 5 deletions

View File

@ -627,7 +627,7 @@ describe('api: options', () => {
render(h(Comp), root) render(h(Comp), root)
instance.foo = 1 instance.foo = 1
expect( expect(
'Computed property "foo" was assigned to but it has no setter.' 'Write operation failed: computed property "foo" is readonly'
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })

View File

@ -40,7 +40,8 @@ import {
reactive, reactive,
ComputedGetter, ComputedGetter,
WritableComputedOptions, WritableComputedOptions,
ComputedRef ComputedRef,
toRaw
} from '@vue/reactivity' } from '@vue/reactivity'
import { import {
ComponentObjectPropsOptions, ComponentObjectPropsOptions,
@ -276,11 +277,12 @@ export function applyOptions(
errorCaptured errorCaptured
} = options } = options
const renderContext = const renderContext = toRaw(
instance.renderContext === EMPTY_OBJ && instance.renderContext === EMPTY_OBJ &&
(computedOptions || methods || watchOptions || injectOptions) (computedOptions || methods || watchOptions || injectOptions)
? (instance.renderContext = reactive({})) ? (instance.renderContext = reactive({}))
: instance.renderContext : instance.renderContext
)
const globalMixins = instance.appContext.mixins const globalMixins = instance.appContext.mixins
// call it only during dev // call it only during dev
@ -355,7 +357,7 @@ export function applyOptions(
: __DEV__ : __DEV__
? () => { ? () => {
warn( warn(
`Computed property "${key}" was assigned to but it has no setter.` `Write operation failed: computed property "${key}" is readonly.`
) )
} }
: NOOP : NOOP
@ -369,7 +371,9 @@ export function applyOptions(
if (renderContext[key] && !(key in proxyTarget)) { if (renderContext[key] && !(key in proxyTarget)) {
Object.defineProperty(proxyTarget, key, { Object.defineProperty(proxyTarget, key, {
enumerable: true, enumerable: true,
get: () => (renderContext[key] as ComputedRef).value configurable: true,
get: () => (renderContext[key] as ComputedRef).value,
set: NOOP
}) })
} }
} }