Home > Web Front-end > JS Tutorial > body text

Under what circumstances is the render function in Vue suitable for use?

不言
Release: 2018-10-11 17:17:07
forward
4154 people have browsed it

What this article brings to you is about under what circumstances is the render function in vue suitable for use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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 a time, the button has four styles (success, error, warning, default ). First of all, you may think of the following implementation

  <div class="btn btn-success" v-if="type === &#39;success&#39;">{{ text }}</div>
  <div class="btn btn-danger" v-else-if="type === &#39;danger&#39;">{{ text }}</div>
  <div class="btn btn-warning" v-else-if="type === &#39;warning&#39;">{{ text }}</div>
Copy after login

There is no problem at all when there are few button styles, but just imagine if there are more than ten button styles required. 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 {
  props: {
    type: {
      type: String,
      default: &#39;normal&#39;
    },
    text: {
      type: String,
      default: &#39;normal&#39;
    }
  },
  computed: {
    tag() {
      switch (this.type) {
        case &#39;success&#39;:
          return 1;
        case &#39;danger&#39;:
          return 2;
        case &#39;warning&#39;:
          return 3;
        default:
          return 1;
      }
    }
  },
  render(h) {
    return h(&#39;p&#39;, {
      class: {
        btn: true,
        &#39;btn-success&#39;: this.type === &#39;success&#39;,
        &#39;btn-danger&#39;: this.type === &#39;danger&#39;,
        &#39;btn-warning&#39;: this.type === &#39;warning&#39;
      },
      domProps: {
        innerText: this.text
      },
      on: {
        click: this.handleClick
      }
    });
  },
  methods: {
    handleClick() {
      console.log(&#39;-----------------------&#39;);
      console.log(&#39;do something&#39;);
    }
  }
};
Copy after login

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

<Button type="danger" text="test"></Button>
Copy after login

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.

  render() {
    return (
      <div
        class={{
          btn: true,
          &#39;btn-success&#39;: this.type === &#39;success&#39;,
          &#39;btn-danger&#39;: this.type === &#39;danger&#39;,
          &#39;btn-warning&#39;: this.type === &#39;warning&#39;
        }}
        onClick={this.handleClick}>
        {this.text}
      </div>
    );
  },
Copy after login

The above is the detailed content of Under what circumstances is the render function in Vue suitable for use?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template