This article mainly introduces how to use the render function in Vue. Now I will share it with you and give you a reference.
render function
vue creates your HTML through template. However, in special cases, this hard-coded model cannot meet the needs, and js programming capabilities must be required. At this point, you need to use render to create HTML.
When is it appropriate to use the render function?
In the process of encapsulating a set of common button components at one time, the button has four styles (default success error). First of all, you may think of the following implementation
success
error
warm
default
There is no problem at all when there are few button styles, but just imagine, if there are more than ten button styles required, the text in the button will be determined according to the actual situation ( For example, the text in the success button may be OK, GOOD, etc.). Then the hard-coded template method seems very weak. In situations like this, using the render function can be said to be the best choice.
Rewrite the button component according to the actual situation
First of all, the content generated by the render function is equivalent to the content of the template. Therefore, when using the render function, you need to first put it in the .vue file. Remove the template tag. Only the logical layer remains.
export default { render(h) { return h('p',{ 'class': { btn: true, success: this.type === 'success', error: this.type === 'error', warm: this.type === 'warm', default: this.type === 'default' }, domProps: { innerHTML: this.$slots.default[0].text }, on: { click: this.clickHandle } }) }, methods: { clickHandle() { // dosomething } }, props: { type: { type: String, default: 'default' }, text: { type: String, default: 'default' } } };
According to component-based thinking, things that can be abstracted are never hard-coded in logic. The clickHandle function here can trigger different logic according to the type of the button, so I won’t go into details.
Then call the parent component
{{btn.text}}
Use jsx
Yes, remember the type of each parameter and the same usage, and pass the parameters in order It's really too much trouble. Then you can actually use jsx to optimize this tedious process.
return ({this.$slots.default[0].text}
)
Example 2:
When you encounter writing similar components, you need to write a lot of long code, from the perspective of simplicity (laziness makes people progress) Therefore, we should find a more suitable method to achieve this effect.
这是h2元素
At this time, the Render function solves this problem very well. Let’s start with a simple example. It has a basic skeleton.
render function
Then further add what you want to your component. The style requires events to become flesh and blood
render function
Note: VNodes in the constraint component must be unique.
It is very painful to directly write all elements under one createElement() and is not conducive to maintenance.
So usually
var com1= createElement('p','item1');var com2= createElement('p','item1');
You can use return createElement('p',[com1,com2])
This situation is prohibited return createElement('p', [com1,com1])
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to implement mysql transaction automatic recycling connection in Node.js
How to delete a certain object in a JS array Element
Details introduction to the knowledge points about promise in js
The above is the detailed content of Basic usage of render function in Vue (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!