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 Environmental Description Docke...
In the previous article, we introduced: MySQL8.0....
1 Cause After the project migrated the database a...
Table of contents Abstraction and reuse Serial Se...
This work uses the knowledge of front-end develop...
Table of contents topic analyze Basic solution Ba...
1. First stop the mysql service As an administrat...
I believe everyone is familiar with database inde...
1. To optimize the query, try to avoid full table...
To learn content-type, you must first know what i...
1. Download the accelerated version of msyql dock...
1. Download, install and activate CLion Just foll...
1. Prepare the Java environment, jdk1.8 Check whe...
Without further ado, let’s run the screenshot dir...
1. Preparation 1.1 harbor download harbor downloa...