Skip to content

Commit 2fb7a63

Browse files
committed
fix(computed): support arrow function usage for computed option
fix #733
1 parent 9571ede commit 2fb7a63

File tree

3 files changed

+4
-6
lines changed

3 files changed

+4
-6
lines changed

packages/reactivity/src/computed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface WritableComputedRef<T> extends Ref<T> {
1111
readonly effect: ReactiveEffect<T>
1212
}
1313

14-
export type ComputedGetter<T> = () => T
14+
export type ComputedGetter<T> = (ctx?: any) => T
1515
export type ComputedSetter<T> = (v: T) => void
1616

1717
export interface WritableComputedOptions<T> {

packages/runtime-core/__tests__/apiOptions.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ describe('api: options', () => {
5252
bar(): number {
5353
return this.foo + 1
5454
},
55-
baz(): number {
56-
return this.bar + 1
57-
}
55+
baz: (vm): number => vm.bar + 1
5856
},
5957
render() {
6058
return h(

packages/runtime-core/src/apiOptions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ export function applyOptions(
302302
__DEV__ && checkDuplicateProperties!(OptionTypes.COMPUTED, key)
303303

304304
if (isFunction(opt)) {
305-
renderContext[key] = computed(opt.bind(ctx))
305+
renderContext[key] = computed(opt.bind(ctx, ctx))
306306
} else {
307307
const { get, set } = opt
308308
if (isFunction(get)) {
309309
renderContext[key] = computed({
310-
get: get.bind(ctx),
310+
get: get.bind(ctx, ctx),
311311
set: isFunction(set)
312312
? set.bind(ctx)
313313
: __DEV__

0 commit comments

Comments
 (0)