]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(computed): support arrow function usage for computed option
authorEvan You <yyx990803@gmail.com>
Tue, 18 Feb 2020 04:22:25 +0000 (23:22 -0500)
committerEvan You <yyx990803@gmail.com>
Tue, 18 Feb 2020 04:22:25 +0000 (23:22 -0500)
fix #733

packages/reactivity/src/computed.ts
packages/runtime-core/__tests__/apiOptions.spec.ts
packages/runtime-core/src/apiOptions.ts

index c3c2a3ad94ef28cd5bdccfd5bb80dcffbbcbbf7e..1a64bdf2a630df35eae43b71e231fd0b2a9c8cd1 100644 (file)
@@ -11,7 +11,7 @@ export interface WritableComputedRef<T> extends Ref<T> {
   readonly effect: ReactiveEffect<T>
 }
 
-export type ComputedGetter<T> = () => T
+export type ComputedGetter<T> = (ctx?: any) => T
 export type ComputedSetter<T> = (v: T) => void
 
 export interface WritableComputedOptions<T> {
index c55c197e3583020ff2b86cec446ccda08053aa40..a7d5ca6b93bff577a41421ad6db92ba5f003536e 100644 (file)
@@ -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(
index 3ba88f7eda94df674612ecc496450e4d4beccdce..e4daea84b6a685ec40a69ed901926b038d247413 100644 (file)
@@ -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__