From 2fb7a63943d9d995248cb6d2d4fb5f22ff2ac000 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 17 Feb 2020 23:22:25 -0500 Subject: [PATCH] fix(computed): support arrow function usage for computed option fix #733 --- packages/reactivity/src/computed.ts | 2 +- packages/runtime-core/__tests__/apiOptions.spec.ts | 4 +--- packages/runtime-core/src/apiOptions.ts | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index c3c2a3ad..1a64bdf2 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -11,7 +11,7 @@ export interface WritableComputedRef extends Ref { readonly effect: ReactiveEffect } -export type ComputedGetter = () => T +export type ComputedGetter = (ctx?: any) => T export type ComputedSetter = (v: T) => void export interface WritableComputedOptions { diff --git a/packages/runtime-core/__tests__/apiOptions.spec.ts b/packages/runtime-core/__tests__/apiOptions.spec.ts index c55c197e..a7d5ca6b 100644 --- a/packages/runtime-core/__tests__/apiOptions.spec.ts +++ b/packages/runtime-core/__tests__/apiOptions.spec.ts @@ -52,9 +52,7 @@ describe('api: options', () => { bar(): number { return this.foo + 1 }, - baz(): number { - return this.bar + 1 - } + baz: (vm): number => vm.bar + 1 }, render() { return h( diff --git a/packages/runtime-core/src/apiOptions.ts b/packages/runtime-core/src/apiOptions.ts index 3ba88f7e..e4daea84 100644 --- a/packages/runtime-core/src/apiOptions.ts +++ b/packages/runtime-core/src/apiOptions.ts @@ -302,12 +302,12 @@ export function applyOptions( __DEV__ && checkDuplicateProperties!(OptionTypes.COMPUTED, key) if (isFunction(opt)) { - renderContext[key] = computed(opt.bind(ctx)) + renderContext[key] = computed(opt.bind(ctx, ctx)) } else { const { get, set } = opt if (isFunction(get)) { renderContext[key] = computed({ - get: get.bind(ctx), + get: get.bind(ctx, ctx), set: isFunction(set) ? set.bind(ctx) : __DEV__