8000 fix: the parent setup should be called first (#276) · pikax/vue-function-api@e1491f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1491f2

Browse files
authored
fix: the parent setup should be called first (vuejs#276)
1 parent 8cea63a commit e1491f2

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/install.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { VueConstructor } from 'vue';
77
/**
88
* Helper that recursively merges two data objects together.
99
*/
10-
function mergeData(to: AnyObject, from?: AnyObject): Object {
10+
function mergeData(from: AnyObject, to: AnyObject): Object {
1111
if (!from) return to;
1212
let key: any;
1313
let toVal: any;
@@ -28,7 +28,7 @@ function mergeData(to: AnyObject, from?: AnyObject): Object {
2828
(isPlainObject(toVal) && !isRef(toVal)) &&
2929
(isPlainObject(fromVal) && !isRef(fromVal))
3030
) {
31-
mergeData(toVal, fromVal);
31+
mergeData(fromVal, toVal);
3232
}
3333
}
3434
return to;
@@ -45,8 +45,8 @@ export function install(Vue: VueConstructor, _install: (Vue: VueConstructor) =>
4545
Vue.config.optionMergeStrategies.setup = function(parent: Function, child: Function) {
4646
return function mergedSetupFn(props: any, context: any) {
4747
return mergeData(
48-
typeof child === 'function' ? child(props, context) || {} : {},
49-
typeof parent === 'function' ? parent(props, context) || {} : {}
48+
typeof parent === 'function' ? parent(props, context) || {} : {},
49+
typeof child === 'function' ? child(props, context) || {} : {}
5050
);
5151
};
5252
};

test/setup.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Vue = require('vue/dist/vue.common.js');
2-
const { ref, computed, createElement: h } = require('../src');
2+
const { ref, computed, createElement: h, provide, inject } = require('../src');
33

44
describe('setup', () => {
55
beforeEach(() => {
@@ -144,14 +144,21 @@ describe('setup', () => {
144144
});
145145

146146
it('should merge result properly', () => {
147+
const injectKey = Symbol('foo');
147148
const A = Vue.extend({
148149
setup() {
150+
provide(injectKey, 'foo');
149151
return { a: 1 };
150152
},
151153
});
152154
const Test = Vue.extend({
153155
extends: A,
154-
setup() {},
156+
setup() {
157+
const injectVal = inject(injectKey);
158+
return {
159+
injectVal,
160+
};
161+
},
155162
});
156163
let vm = new Test({
157164
setup() {
@@ -160,6 +167,7 @@ describe('setup', () => {
160167
});
161168
expect(vm.a).toBe(1);
162169
expect(vm.b).toBe(2);
170+
expect(vm.injectVal).toBe('foo');
163171
// no instance data
164172
vm = new Test();
165173
expect(vm.a).toBe(1);

0 commit comments

Comments
 (0)
0