]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): respect render function from mixins
authorEvan You <yyx990803@gmail.com>
Mon, 27 Jul 2020 21:44:17 +0000 (17:44 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 27 Jul 2020 21:44:17 +0000 (17:44 -0400)
fix #1630

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

index 0107848ec68ecb5a82014d3352480dcddb5dfc33..074596bb30f128e2e1fc5ee06505292c59566df3 100644 (file)
@@ -520,6 +520,17 @@ describe('api: options', () => {
     ])
   })
 
+  test('render from mixin', () => {
+    const Comp = {
+      mixins: [
+        {
+          render: () => 'from mixin'
+        }
+      ]
+    }
+    expect(renderToString(h(Comp))).toBe('from mixin')
+  })
+
   test('extends', () => {
     const calls: string[] = []
     const Base = {
index 2a8e1efbe6377f135d952e8fdf720861fa5a2efa..58e1725e8e9a251b9c9605aae8f8414c92e18c53 100644 (file)
@@ -589,6 +589,7 @@ function finishComponentSetup(
       instance.render = Component.render as InternalRenderFunction
     }
   } else if (!instance.render) {
+    // could be set from setup()
     if (compile && Component.template && !Component.render) {
       if (__DEV__) {
         startMeasure(instance, `compile`)
@@ -604,25 +605,6 @@ function finishComponentSetup(
       ;(Component.render as InternalRenderFunction)._rc = true
     }
 
-    if (__DEV__ && !Component.render) {
-      /* istanbul ignore if */
-      if (!compile && Component.template) {
-        warn(
-          `Component provided template option but ` +
-            `runtime compilation is not supported in this build of Vue.` +
-            (__ESM_BUNDLER__
-              ? ` Configure your bundler to alias "vue" to "vue/dist/vue.esm-bundler.js".`
-              : __ESM_BROWSER__
-                ? ` Use "vue.esm-browser.js" instead.`
-                : __GLOBAL__
-                  ? ` Use "vue.global.js" instead.`
-                  : ``) /* should not happen */
-        )
-      } else {
-        warn(`Component is missing template or render function.`)
-      }
-    }
-
     instance.render = (Component.render || NOOP) as InternalRenderFunction
 
     // for runtime-compiled render functions using `with` blocks, the render
@@ -642,6 +624,26 @@ function finishComponentSetup(
     applyOptions(instance, Component)
     currentInstance = null
   }
+
+  // warn missing template/render
+  if (__DEV__ && !Component.render && instance.render === NOOP) {
+    /* istanbul ignore if */
+    if (!compile && Component.template) {
+      warn(
+        `Component provided template option but ` +
+          `runtime compilation is not supported in this build of Vue.` +
+          (__ESM_BUNDLER__
+            ? ` Configure your bundler to alias "vue" to "vue/dist/vue.esm-bundler.js".`
+            : __ESM_BROWSER__
+              ? ` Use "vue.esm-browser.js" instead.`
+              : __GLOBAL__
+                ? ` Use "vue.global.js" instead.`
+                : ``) /* should not happen */
+      )
+    } else {
+      warn(`Component is missing template or render function.`)
+    }
+  }
 }
 
 const attrHandlers: ProxyHandler<Data> = {
index 247ea946d47ac574b3444433c9a93a608a603f43..19e8f4bbba0873c29e966b1d43a3dc008ca65ab1 100644 (file)
@@ -4,7 +4,8 @@ import {
   SetupContext,
   ComponentInternalOptions,
   PublicAPIComponent,
-  Component
+  Component,
+  InternalRenderFunction
 } from './component'
 import {
   isFunction,
@@ -390,6 +391,7 @@ export function applyOptions(
     deactivated,
     beforeUnmount,
     unmounted,
+    render,
     renderTracked,
     renderTriggered,
     errorCaptured
@@ -398,7 +400,10 @@ export function applyOptions(
   const publicThis = instance.proxy!
   const ctx = instance.ctx
   const globalMixins = instance.appContext.mixins
-  // call it only during dev
+
+  if (asMixin && render && instance.render === NOOP) {
+    instance.render = render as InternalRenderFunction
+  }
 
   // applyOptions is called non-as-mixin once per instance
   if (!asMixin) {
@@ -406,6 +411,7 @@ export function applyOptions(
     // global mixins are applied first
     applyMixins(instance, globalMixins, deferredData, deferredWatch)
   }
+
   // extending a base component...
   if (extendsOptions) {
     applyOptions(instance, extendsOptions, deferredData, deferredWatch, true)