From 8d99ab1ff8afabe1dcbf346bd6174084e0911261 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 21 Aug 2019 09:50:20 -0400 Subject: [PATCH] chore: allow custom assignment to this --- packages/runtime-core/src/component.ts | 6 ++++++ packages/runtime-core/src/componentProxy.ts | 18 +++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 25c2f6b70..724718115 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -115,6 +115,9 @@ export type ComponentInstance

= { renderProxy: ComponentRenderProxy | null propsProxy: P | null setupContext: SetupContext | null + + // user namespace + user: { [key: string]: any } } & SetupContext & LifecycleHooks @@ -198,6 +201,9 @@ export function createComponentInstance( slots: EMPTY_OBJ, refs: EMPTY_OBJ, + // user namespace for storing whatever the user assigns to `this` + user: {}, + emit: (event: string, ...args: unknown[]) => { const props = instance.vnode.props || EMPTY_OBJ const handler = props[`on${event}`] || props[`on${capitalize(event)}`] diff --git a/packages/runtime-core/src/componentProxy.ts b/packages/runtime-core/src/componentProxy.ts index 348740a34..2d37da1fa 100644 --- a/packages/runtime-core/src/componentProxy.ts +++ b/packages/runtime-core/src/componentProxy.ts @@ -26,7 +26,7 @@ export const RenderProxyHandlers = { case '$emit': return target.emit default: - break + return target.user[key] } } }, @@ -35,15 +35,15 @@ export const RenderProxyHandlers = { if (data.hasOwnProperty(key)) { data[key] = value return true + } else if (key[0] === '$' && key.slice(1) in target) { + // TODO warn attempt of mutating public property + return false + } else if (key in target.props) { + // TODO warn attempt of mutating prop + return false } else { - if (__DEV__) { - if (key[0] === '$') { - // TODO warn attempt of mutating public property - } else if (target.props.hasOwnProperty(key)) { - // TODO warn attempt of mutating prop - } - } + target.user[key] = value + return true } - return false } }