fix(computed): support arrow function usage for computed option

fix #733
This commit is contained in:
Evan You 2020-02-17 23:22:25 -05:00
parent 9571ede84b
commit 2fb7a63943
3 changed files with 4 additions and 6 deletions

View File

@ -11,7 +11,7 @@ export interface WritableComputedRef<T> extends Ref<T> {
readonly effect: ReactiveEffect<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 type ComputedSetter<T> = (v: T) => void
export interface WritableComputedOptions<T> { export interface WritableComputedOptions<T> {

View File

@ -52,9 +52,7 @@ describe('api: options', () => {
bar(): number { bar(): number {
return this.foo + 1 return this.foo + 1
}, },
baz(): number { baz: (vm): number => vm.bar + 1
return this.bar + 1
}
}, },
render() { render() {
return h( return h(

View File

@ -302,12 +302,12 @@ export function applyOptions(
__DEV__ && checkDuplicateProperties!(OptionTypes.COMPUTED, key) __DEV__ && checkDuplicateProperties!(OptionTypes.COMPUTED, key)
if (isFunction(opt)) { if (isFunction(opt)) {
renderContext[key] = computed(opt.bind(ctx)) renderContext[key] = computed(opt.bind(ctx, ctx))
} else { } else {
const { get, set } = opt const { get, set } = opt
if (isFunction(get)) { if (isFunction(get)) {
renderContext[key] = computed({ renderContext[key] = computed({
get: get.bind(ctx), get: get.bind(ctx, ctx),
set: isFunction(set) set: isFunction(set)
? set.bind(ctx) ? set.bind(ctx)
: __DEV__ : __DEV__