Component Basics 1 Component ReuseComponents are reusable Vue instances. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <button-counter></button-counter> <button-counter></button-counter> <button-counter></button-counter> </div> <script> // Define a new component called button-counter Vue.component('button-counter', { data: function () { return { count: 0 } }, template: '<button v-on:click="count++">Clicked {{ count }} times.</button>' }); new Vue({ el: '#app' }); </script> </body> </html> Note that when the button is clicked, each component will maintain its count independently. Here the data property of the custom component must be a function, and each instance maintains an independent copy of the returned object. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <button-counter></button-counter> <button-counter></button-counter> <button-counter></button-counter> </div> <script> var buttonCounterData = { count: 0 } // Define a new component called button-counter Vue.component('button-counter', { data: function () { return buttonCounterData }, template: '<button v-on:click="count++">Clicked {{ count }} times.</button>' }); new Vue({ el: '#app' }); </script> </body> </html> 2 Passing data to child components through Props<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <blog-post title="My journey with Vue"></blog-post> <blog-post title="Blogging with Vue"></blog-post> <blog-post title="Why Vue is so fun"></blog-post> </div> <script> Vue.component('blog-post', { props: ['title'], template: '<h3>{{ title }}</h3>' }) new Vue({ el: '#app' }); </script> </body> </html> Here, <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:title="post.title"></blog-post> </div> <script> Vue.component('blog-post', { props: ['title'], template: '<h3>{{ title }}</h3>' }) new Vue({ el: '#app', data: { posts: { id: 1, title: 'My journey with Vue' }, { id: 2, title: 'Blogging with Vue' }, { id: 3, title: 'Why Vue is so fun' } ] } }); </script> </body> </html> 3 Single root elementEach component must have only one root element. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post"></blog-post> </div> <script> Vue.component('blog-post', { props: ['post'], template: ` <div class="blog-post"> <h3>{{ post.title }}</h3> <div v-html="post.content"></div> </div> ` }) new Vue({ el: '#app', data: { posts: { id: 1, title: 'My journey with Vue', content: 'my journey...' }, { id: 2, title: 'Blogging with Vue', content: 'my blog...' }, { id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' } ] } }); </script> </body> </html> Note that the post bound by v-bind:post="post" is an object, which avoids the trouble of passing data through many props. 4 Listening for subcomponent events<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <div :style="{fontSize: postFontSize + 'em'}"> <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post" v-on:enlarge-text="postFontSize += 0.1" /> </div> </div> <script> Vue.component('blog-post', { props: ['post'], template: ` <div class="blog-post"> <h3>{{ post.title }}</h3> <button v-on:click="$emit('enlarge-text')">Enlarge the text</button> <div v-html="post.content"></div> </div> ` }) new Vue({ el: '#app', data: { postFontSize: 1, posts: { id: 1, title: 'My journey with Vue', content: 'my journey...' }, { id: 2, title: 'Blogging with Vue', content: 'my blog...' }, { id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' } ] } }); </script> </body> </html> The child component triggers an event by using the We can use events to throw a value. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <div :style="{fontSize: postFontSize + 'em'}"> <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post" v-on:enlarge-text="postFontSize += $event" /> </div> </div> <script> Vue.component('blog-post', { props: ['post'], template: ` <div class="blog-post"> <h3>{{ post.title }}</h3> <button v-on:click="$emit('enlarge-text', 0.2)">Enlarge the font</button> <div v-html="post.content"></div> </div> ` }) new Vue({ el: '#app', data: { postFontSize: 1, posts: { id: 1, title: 'My journey with Vue', content: 'my journey...' }, { id: 2, title: 'Blogging with Vue', content: 'my blog...' }, { id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' } ] } }); </script> </body> </html> In the parent component, we can access the thrown value through $event. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <!-- <input v-model="searchText"> --> <input v-bind:value="searchText" v-on:input="searchText = $event.target.value"> <p>{{ searchText }}</p> </div> <script> new Vue({ el: '#app', data: { searchText: '' } }); </script> </body> </html> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> </style> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> </head> <body> <div id="app"> <custom-input v-model="searchText"></custom-input> <custom-input v-bind:value="searchText" v-on:input="searchText = $event"></custom-input> <p>{{ searchText }}</p> </div> <script> Vue.component('custom-input', { props: ['value'], template: `<input v-bind:value="value" v-on:input="$emit('input', $event.target.value)" >` }) new Vue({ el: '#app', data: { searchText: '' } }); </script> </body> </html> Finally, a note of caution when parsing DOM templates. The above is the detailed content of the summary of the basic knowledge of Vue components. For more information about Vue components, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: What is ZFS? Reasons to use ZFS and its features
>>: Linux installation MySQL tutorial (binary distribution)
Table of contents Preface First look at React Con...
Requirements: The PC side and the mobile side are...
1. Indexes do not store null values More precisel...
This article shares the specific code for JavaScr...
I recently used Dreamweaver to make a product pres...
1. Install Docker 1. I installed Centos7 in the v...
I have previously shared the usage of asynchronou...
<table id=" <%=var1%>">, the...
Table of contents 1. Download the tomcat code 2. ...
First download JDK. Here we use jdk-8u181-linux-x...
Preface In order to reflect the difference betwee...
It has been a long time since the last update of ...
The fixed IP address of the centos-DVD1 version s...
This article is mainly to let beginners understan...
The so-called sliding door technology means that ...