8000 fix: make setup works like data · e3d/vue-function-api@e161644 · GitHub
[go: up one dir, main page]

Skip to content

Commit e161644

Browse files
antfuliximomo
authored andcommitted
fix: make setup works like data
1 parent 109b877 commit e161644

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/setup.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { VueConstructor } from 'vue';
22
import { Context } from './types/vue';
33
import { isWrapper } from './helper';
44
import { setCurrentVM } from './runtimeContext';
5-
import { isPlainObject, assert, proxy } from './utils';
5+
import { isPlainObject, assert, proxy, isFunction } from './utils';
6+
import { value } from './functions/state';
67

78
export function mixin(Vue: VueConstructor) {
89
Vue.mixin({
@@ -56,7 +57,14 @@ export function mixin(Vue: VueConstructor) {
5657
}
5758

5859
Object.keys(binding).forEach(name => {
59-
const bindingValue = binding[name];
60+
let bindingValue = binding[name];
61+
if (bindingValue === undefined)
62+
return;
63+
// make plain value reactive
64+
if (!isWrapper(bindingValue) && !isFunction(bindingValue) && !Object.isFrozen(bindingValue)) {
65+
bindingValue = value(bindingValue);
66+
}
67+
// bind to vm
6068
if (isWrapper(bindingValue)) {
6169
bindingValue.setVmProperty(vm, name);
6270
} else {

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ export function isArray<T>(x: unknown): x is T[] {
3333
export function isPlainObject<T extends Object = {}>(x: unknown): x is T {
3434
return toString(x) === '[object Object]';
3535
}
36+
37+
export function isFunction(x: unknown): x is Function {
38+
return typeof x === 'function';
39+
}

test/setup.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,45 @@ describe('setup', () => {
236236
},
237237
}).$mount();
238238
});
239+
240+
it('should make returned plain value reactive (value)', done => {
241+
const vm = new Vue({
242+
setup() {
243+
return {
244+
name: null,
245+
nested: {
246+
object: {
247+
msg: 'foo'
248+
}
249+
},
250+
};
251+
},
252+
template: '<div>{{ name }}, {{ nested.object.msg }}</div>',
253+
}).$mount();
254+
expect(vm.$el.textContent).toBe(', foo');
255+
vm.name = 'foo';
256+
vm.nested.object.msg = 'bar';
257+
waitForUpdate(() => {
258+
expect(vm.$el.textContent).toBe('foo, bar');
259+
}).then(done);
260+
})
261+
262+
it('should make returned plain value reactive (object)', done => {
263+
const vm = new Vue({
264+
setup() {
265+
return {
266+
form: {
267+
a: 1,
268+
b: 2
269+
}
270+
};
271+
},
272+
template: '<div>{{ form.a }}, {{ form.b }}</div>',
273+
}).$mount();
274+
expect(vm.$el.textContent).toBe('1, 2');
275+
vm.form = { a: 2, b: 3 };
276+
waitForUpdate(() => {
277+
expect(vm.$el.textContent).toBe('2, 3');
278+
}).then(done);
279+
})
239280
});

0 commit comments

Comments
 (0)
0