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)
1. Introduction The topic of whether to use forei...
Preface The this pointer in JS has always been a ...
What is HTML? HTML is a language used to describe...
1. The role of doctype, the difference between st...
Table of contents React upload file display progr...
1. Introduction Are you still leaving your websit...
Table of contents introduce Prepare Download syst...
In web front-end development, it is inevitable to ...
question: The following error occurred when insta...
Basic Use <!DOCTYPE html> <html lang=&qu...
Starting from IE 8, IE added a compatibility mode,...
First create a directory cd /etc/nginx mkdir ssl ...
Table of contents 1. Component switching method M...
In more and more websites, the use of XHTML is rep...
This article is part of a special series on the 2...