Skip to content

Commit

Permalink
fix: the parent setup should be called first (vuejs#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaonialife authored and pikax committed Apr 19, 2020
1 parent 1f2c85a commit 8ff317e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { VueConstructor } from 'vue';
/**
* Helper that recursively merges two data objects together.
*/
function mergeData(to: AnyObject, from?: AnyObject): Object {
function mergeData(from: AnyObject, to: AnyObject): Object {
if (!from) return to;
let key: any;
let toVal: any;
Expand All @@ -28,7 +28,7 @@ function mergeData(to: AnyObject, from?: AnyObject): Object {
(isPlainObject(toVal) && !isRef(toVal)) &&
(isPlainObject(fromVal) && !isRef(fromVal))
) {
mergeData(toVal, fromVal);
mergeData(fromVal, toVal);
}
}
return to;
Expand All @@ -45,8 +45,8 @@ export function install(Vue: VueConstructor, _install: (Vue: VueConstructor) =>
Vue.config.optionMergeStrategies.setup = function(parent: Function, child: Function) {
return function mergedSetupFn(props: any, context: any) {
return mergeData(
typeof child === 'function' ? child(props, context) || {} : {},
typeof parent === 'function' ? parent(props, context) || {} : {}
typeof parent === 'function' ? parent(props, context) || {} : {},
typeof child === 'function' ? child(props, context) || {} : {}
);
};
};
Expand Down
12 changes: 10 additions & 2 deletions test/setup.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Vue = require('vue/dist/vue.common.js');
const { ref, computed, createElement: h } = require('../src');
const { ref, computed, createElement: h, provide, inject } = require('../src');

describe('setup', () => {
beforeEach(() => {
Expand Down Expand Up @@ -144,14 +144,21 @@ describe('setup', () => {
});

it('should merge result properly', () => {
const injectKey = Symbol('foo');
const A = Vue.extend({
setup() {
provide(injectKey, 'foo');
return { a: 1 };
},
});
const Test = Vue.extend({
extends: A,
setup() {},
setup() {
const injectVal = inject(injectKey);
return {
injectVal,
};
},
});
let vm = new Test({
setup() {
Expand All @@ -160,6 +167,7 @@ describe('setup', () => {
});
expect(vm.a).toBe(1);
expect(vm.b).toBe(2);
expect(vm.injectVal).toBe('foo');
// no instance data
vm = new Test();
expect(vm.a).toBe(1);
Expand Down

0 comments on commit 8ff317e

Please sign in to comment.