At the HTML level, we decided to define the component interface as follows: generate h1-h6 tags by passing different levels 1-6, and use slots to generate content <div id="div1"> <child :level="1">Hello world!</child> </div> <script type="text/x-template" id="child-template"> <h1 v-if="level === 1"> <slot></slot> </h1> <h2 v-if="level === 2"> <slot></slot> </h2> <h3 v-if="level === 3"> <slot></slot> </h3> <h4 v-if="level === 4"> <slot></slot> </h4> <h5 v-if="level === 5"> <slot></slot> </h5> <h6 v-if="level === 6"> <slot></slot> </h6> </script> <script type="text/javascript"> /** * Globally register the child component. Note that if the template value starts with #, it is used as an option symbol and the innerHTML of the matching element will be used as the template. A common trick is to use <script type="x-template"> to include the template. The advantage of this is that the HTML will not render the content inside* * Using template here is not the best choice: * 1. The code is lengthy* 2. Inserting content in different titles requires repeated use of slots * 3. Since components must have a root element, the title and content are wrapped in a useless div, such as <div><h1>hello world</h1></div> */ Vue.component('child', { template: '#child-template', props: { level: type: Number, required: true } }, data: function() { return { a: 1 } } }) new Vue({ el:"#div1" }) </script> We try to implement the above example using the render function. Note that when using the render function, the template option will be ignored.
Here is an example: <div id="div1"> <child :level="1"> Hello world! </child> <child :level="2"> <!-- will not be displayed --> <span slot="footer">this is footer slot</span> <p slot="header">this is header slot</p> </child> </div> <script> Vue.component('child', { render: function (createElement) { console.log(this.$slots); return createElement( 'h' + this.level, // tagName tag name { // Set the class for each h tag 'class': { foo: true, bar: false }, //Finally rendered as inline style style: { color: 'red', fontSize: '14px' }, // Other HTML attributes attrs: { id: 'foo', 'data-id': 'bar' }, // DOM attributes domProps: { // innerHTML: 'from domProps', }, // Event listener based on "on" // So no longer supports modifiers such as v-on:keyup.enter on: { click: this.clickHandler }, // ... }, // You can get the static content in the VNodes list from this.$slots // $slots.default is used to access the unnamed slot of the component // When you may need a named slot, you need to specify the slot name, this.$slots.header // [this.$slots.default,this.$slots.footer,this.$slots.header] //Display level 2 slots [this.$slots.default] //Display only unnamed slots ) }, template: '<div v-if="level===1"><slot></slot></div>', // will be ignored props: { level: type: Number, required: true } }, methods: { clickHandler: function () { console.log('clickHandler') } } }) new Vue({ el: "#div1" }) </script> The rendering is as follows: This is the end of this article about the detailed use of Vue.js's render function. For more relevant Vue.js's render function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to detect file system integrity based on AIDE in Linux
>>: Solutions to MySQL OOM (memory overflow)
<a href="https://www.jb51.net/" titl...
Table of contents How to represent the current ti...
This article uses a specific example to introduce...
Download foreign images using Alibaba Cloud Image...
Table of contents Preface text 1. Panel 2. Huaron...
1. Unzip the zip package to the installation dire...
This article shares the MySQL installation tutori...
This article example shares the specific code of ...
In fact, this is very simple. We add an a tag to ...
1. Open port 2375 Edit docker.service vim /lib/sy...
1. Python installation 1. Create a folder. mkdir ...
In an article a long time ago, I talked about the...
When developing a website function, the session c...
Problem Description After installing workstations...
This article shares the installation and configur...