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)
HTML web page hyperlink tag learning tutorial lin...
Installation Steps 1. Install Redis Download the ...
This article introduces a tutorial about how to u...
Table of contents 1. Prepare materials 2. Downloa...
There are two types: (different browsers) 1. Avail...
Table of contents 1. Operators Summarize 1. Opera...
Version 1.4.2 Official Documentation dockerhub st...
1. Open the CentOS 7 virtual machine. 2. Log in t...
Table of contents Preface Setting up slow query l...
Today, there is such a requirement. If the logged...
We know that MySQL is a persistent storage, store...
1. Clarify the design direction <br />First,...
First: via text/HTML var txt1="<h1>Tex...
1. Log in to the system and enter the directory: ...
1 CSS style without semicolon ";" 2 Tags...