Skip to content

Commit

Permalink
perf(runtime-core): use raw context on component options init
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 16, 2020
1 parent 24e5ab3 commit bfd6744
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ describe('api: options', () => {
render(h(Comp), root)
instance.foo = 1
expect(
'Computed property "foo" was assigned to but it has no setter.'
'Write operation failed: computed property "foo" is readonly'
).toHaveBeenWarned()
})

Expand Down
12 changes: 8 additions & 4 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import {
reactive,
ComputedGetter,
WritableComputedOptions,
ComputedRef
ComputedRef,
toRaw
} from '@vue/reactivity'
import {
ComponentObjectPropsOptions,
Expand Down Expand Up @@ -276,11 +277,12 @@ export function applyOptions(
errorCaptured
} = options

const renderContext =
const renderContext = toRaw(
instance.renderContext === EMPTY_OBJ &&
(computedOptions || methods || watchOptions || injectOptions)
? (instance.renderContext = reactive({}))
: instance.renderContext
)

const globalMixins = instance.appContext.mixins
// call it only during dev
Expand Down Expand Up @@ -355,7 +357,7 @@ export function applyOptions(
: __DEV__
? () => {
warn(
`Computed property "${key}" was assigned to but it has no setter.`
`Write operation failed: computed property "${key}" is readonly.`
)
}
: NOOP
Expand All @@ -369,7 +371,9 @@ export function applyOptions(
if (renderContext[key] && !(key in proxyTarget)) {
Object.defineProperty(proxyTarget, key, {
enumerable: true,
get: () => (renderContext[key] as ComputedRef).value
configurable: true,
get: () => (renderContext[key] as ComputedRef).value,
set: NOOP
})
}
}
Expand Down

0 comments on commit bfd6744

Please sign in to comment.