1. The createElement Method
The common DOM operations in HTML are modifying DOM nodes and accessing DOM nodes.
Beyond those, the W3C has also published technical standards for creating and deleting DOM nodes.
The createElement method is used to create a DOM node. createElement() usually needs to be used together with the appendChild() or insertBefore() methods.
Among them:
- The appendChild() method is used to insert a new node at the end of the specified child node list.
- The insertBefore() method is used to insert a new node before a specified existing child node.
| |
2. Vue Custom Rendering
In Vue there are two ways to write page rendering:
| |
In fact, when Vue calls the mounted method, it compiles the template into a render method. Rendering with template is less efficient than rendering with render.
Vue’s rendering is functionally similar to the W3C’s createElement method. Vue also has a createElement method. Unlike the W3C’s createElement method, Vue’s createElement method does not operate on the DOM directly, but on VNodes.
3. The h Method in Vue
The h method in Vue is shorthand for the createElement method.
First, let’s analyze how Vue renders:
Vue uses _render to render an instance into a VNode. The main processing logic in _render is:
| |
As you can see, Vue passes vm.$createElement to the render method. And the createElement method is a wrapper around the _createElement method.
| |
Here context is the context environment of the VNode. With this, the parameter list of the createElement method — or the h method — becomes understandable.
| |
