From fa589488255d4de8d04bd14f284131b57534f8dd Mon Sep 17 00:00:00 2001 From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Thu, 26 Nov 2020 08:45:23 +0000 Subject: [PATCH 1/4] docs: expand the page on render functions, especially their use of slots --- src/guide/render-function.md | 150 ++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/src/guide/render-function.md b/src/guide/render-function.md index ab8539a2b4..d91bd290d9 100644 --- a/src/guide/render-function.md +++ b/src/guide/render-function.md @@ -62,9 +62,7 @@ const app = Vue.createApp({}) app.component('anchored-heading', { render() { - const { h } = Vue - - return h( + return Vue.h( 'h' + this.level, // tag name {}, // props/attributes this.$slots.default() // array of children @@ -164,6 +162,8 @@ h( ) ``` +If there are no props then the children can usually be passed as the second argument. In cases where that would be ambiguous, `null` can be passed as the second argument to keep the children as the third argument. + ## Complete Example With this knowledge, we can now finish the component we started: @@ -240,6 +240,45 @@ render() { } ``` +## Creating Component VNodes + +To create a VNode for a component, the first argument passed to `h` should be the component itself: + +```js +render() { + return Vue.h(ButtonCounter) +} +``` + +If we need to resolve a component by name then we can call `resolveComponent`: + +```js +render() { + const ButtonCounter = Vue.resolveComponent('ButtonCounter') + return Vue.h(ButtonCounter) +} +``` + +`resolveComponent` is the same function that templates use internally to resolve components by name. However, it's often unnecessary when writing `render` functions manually. Consider the following example: + +```js +// We can simplify this +components: { + ButtonCounter +}, +render() { + return Vue.h(Vue.resolveComponent('ButtonCounter')) +} +``` + +Rather than registering a component by name and then looking it up we can use it directly instead: + +```js +render() { + return Vue.h(ButtonCounter) +} +``` + ## Replacing Template Features with Plain JavaScript ### `v-if` and `v-for` @@ -268,6 +307,8 @@ render() { } ``` +In a template it can be useful to use a `