Home  >  Article  >  Web Front-end  >  Under what circumstances is the render function in Vue suitable for use?

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

不言
不言forward
2018-10-11 17:11:464152browse

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>

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;);
    }
  }
};

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

2ca5b83124d1c70f8ddaffa0be94a8dda1cb88e6789f399807801ea3799938af

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 (
      3f08712530dae5c6de207c1fb4513106
        {this.text}
      16b28748ea4df4d9c2150843fecfba68
    );
  },

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete