You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/render-function.md
+57-43Lines changed: 57 additions & 43 deletions
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,9 @@ Anchored headings are used very frequently, we should create a component:
21
21
The component must generate a heading based on the `level` prop, and we quickly arrive at this:
22
22
23
23
```js
24
-
constapp=Vue.createApp({})
24
+
const { createApp } = Vue
25
+
26
+
constapp=createApp({})
25
27
26
28
app.component('anchored-heading', {
27
29
template:`
@@ -58,11 +60,13 @@ This template doesn't feel great. It's not only verbose, but we're duplicating `
58
60
While templates work great for most components, it's clear that this isn't one of them. So let's try rewriting it with a `render()` function:
59
61
60
62
```js
61
-
constapp=Vue.createApp({})
63
+
const { createApp, h } = Vue
64
+
65
+
constapp=createApp({})
62
66
63
67
app.component('anchored-heading', {
64
68
render() {
65
-
returnVue.h(
69
+
returnh(
66
70
'h'+this.level, // tag name
67
71
{}, // props/attributes
68
72
this.$slots.default() // array of children
@@ -109,7 +113,7 @@ Or in a render function:
109
113
110
114
```js
111
115
render() {
112
-
returnVue.h('h1', {}, this.blogTitle)
116
+
returnh('h1', {}, this.blogTitle)
113
117
}
114
118
```
115
119
@@ -120,7 +124,7 @@ And in both cases, Vue automatically keeps the page updated, even when `blogTitl
120
124
Vue keeps the page updated by building a **virtual DOM** to keep track of the changes it needs to make to the real DOM. Taking a closer look at this line:
121
125
122
126
```js
123
-
returnVue.h('h1', {}, this.blogTitle)
127
+
returnh('h1', {}, this.blogTitle)
124
128
```
125
129
126
130
What is the `h()` function returning? It's not _exactly_ a real DOM element. It returns a plain object which contains information describing to Vue what kind of node it should render on the page, including descriptions of any child nodes. We call this node description a "virtual node", usually abbreviated to **VNode**. "Virtual DOM" is what we call the entire tree of VNodes, built by a tree of Vue components.
@@ -169,7 +173,9 @@ If there are no props then the children can usually be passed as the second argu
169
173
With this knowledge, we can now finish the component we started:
Behind the scenes, templates use `resolveDynamicComponent` to implement the `is` attribute. We can use the same function if we need all the flexibility provided by `is` in our `render` function:
0 commit comments