]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): should call chained mixins and extends (#3040)
authorHcySunYang <HcySunYang@outlook.com>
Thu, 25 Mar 2021 20:39:57 +0000 (04:39 +0800)
committerGitHub <noreply@github.com>
Thu, 25 Mar 2021 20:39:57 +0000 (16:39 -0400)
fix #3038

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

index 41ce25fdfae6d4d78fec242005b67cdadd238551..9da4e7b03d0d898efa47ee5fdee16745e7ab9338 100644 (file)
@@ -644,6 +644,93 @@ describe('api: options', () => {
     expect(renderToString(h(Comp))).toBe('from mixin')
   })
 
+  test('chained mixins in extends', () => {
+    const calls: string[] = []
+    const mixinA = {
+      beforeCreate() {
+        calls.push('mixinA beforeCreate')
+      },
+      created() {
+        calls.push('mixinA created')
+      }
+    }
+
+    const extendA = {
+      mixins: [mixinA],
+      beforeCreate() {
+        calls.push('extendA beforeCreate')
+      },
+      created() {
+        calls.push('extendA created')
+      }
+    }
+
+    const Comp = {
+      extends: extendA,
+      render: () => '123',
+      beforeCreate() {
+        calls.push('self beforeCreate')
+      },
+      created() {
+        calls.push('self created')
+      }
+    }
+
+    expect(renderToString(h(Comp))).toBe(`123`)
+    expect(calls).toEqual([
+      'mixinA beforeCreate',
+      'extendA beforeCreate',
+      'self beforeCreate',
+      'mixinA created',
+      'extendA created',
+      'self created'
+    ])
+  })
+
+  test('chained extends in mixins', () => {
+    const calls: string[] = []
+
+    const extendA = {
+      beforeCreate() {
+        calls.push('extendA beforeCreate')
+      },
+      created() {
+        calls.push('extendA created')
+      }
+    }
+
+    const mixinA = {
+      extends: extendA,
+      beforeCreate() {
+        calls.push('mixinA beforeCreate')
+      },
+      created() {
+        calls.push('mixinA created')
+      }
+    }
+
+    const Comp = {
+      mixins: [mixinA],
+      render: () => '123',
+      beforeCreate() {
+        calls.push('self beforeCreate')
+      },
+      created() {
+        calls.push('self created')
+      }
+    }
+
+    expect(renderToString(h(Comp))).toBe(`123`)
+    expect(calls).toEqual([
+      'extendA beforeCreate',
+      'mixinA beforeCreate',
+      'self beforeCreate',
+      'extendA created',
+      'mixinA created',
+      'self created'
+    ])
+  })
+
   test('extends', () => {
     const calls: string[] = []
     const Base = {
index 354c9425c4ad6fffc0de4c9235f4ce84628efefe..604531da677cfec584a22bea381699f7d9c8103f 100644 (file)
@@ -839,6 +839,10 @@ function callHookFromExtends(
   if (base.extends) {
     callHookFromExtends(name, type, base.extends, instance)
   }
+  const chainedMixins = base.mixins
+  if (chainedMixins) {
+    callHookFromMixins(name, type, chainedMixins, instance)
+  }
   const baseHook = base[name]
   if (baseHook) {
     callWithAsyncErrorHandling(baseHook.bind(instance.proxy!), instance, type)
@@ -853,6 +857,10 @@ function callHookFromMixins(
 ) {
   for (let i = 0; i < mixins.length; i++) {
     const chainedMixins = mixins[i].mixins
+    const chainedExtends = mixins[i].extends
+    if (chainedExtends) {
+      callHookFromExtends(name, type, chainedExtends, instance)
+    }
     if (chainedMixins) {
       callHookFromMixins(name, type, chainedMixins, instance)
     }