国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

vue 組件基礎(chǔ)知識總結(jié)

瀏覽:3日期:2022-10-08 14:17:29
組件基礎(chǔ)1 組件的復(fù)用

組件是可復(fù)用的Vue實例。

<!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>// 定義一個名為 button-counter 的新組件Vue.component(’button-counter’, {data: function () {return {count: 0}},template: ’<button v-on:click='count++'>點擊了 {{ count }} 次.</button>’});new Vue({ el: ’#app’ }); </script> </body></html>

注意當(dāng)點擊按鈕時,每個組件都會各自獨立維護(hù)它的count。這里自定義組件的data屬性必須是一個函數(shù),每個實例維護(hù)一份被返回對象的獨立的拷貝。

<!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}// 定義一個名為 button-counter 的新組件Vue.component(’button-counter’, {data: function () {return buttonCounterData},template: ’<button v-on:click='count++'>點擊了 {{ count }} 次.</button>’});new Vue({ el: ’#app’ }); </script> </body></html>2 通過 Prop 向子組件傳遞數(shù)據(jù)

<!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></blog-post><blog-post></blog-post><blog-post></blog-post></div> <script>Vue.component(’blog-post’, {props: [’title’],template: ’<h3>{{ title }}</h3>’})new Vue({ el: ’#app’ }); </script> </body></html>

這里<blog-post>組件就是通過自定義屬性title來傳遞數(shù)據(jù)。我們可以使用v-bind來動態(tài)傳遞prop。

<!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 單個根元素

每個組件必須只有一個根元素。

<!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>

注意到v-bind:post='post'綁定的post是一個對象,這樣可以避免了需要通過很多prop傳遞數(shù)據(jù)的麻煩。

4 監(jiān)聽子組件事件

<!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’)'>放大字體</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>

子組件通過$emit方法并傳入事件名稱來觸發(fā)一個事件。父組件可以接收該事件。

我們可以使用事件拋出一個值。

<!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)'>放大字體</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>

在父組件中,我們可以通過$event訪問到被拋出的這個值。我們可以在組件上使用v-model。

<!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>

最后,注意解析 DOM 模板時的注意事項。

以上就是vue 組件基礎(chǔ)知識總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于vue 組件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 久久久一区二区三区 | 在线不卡亚洲 | 亚洲第一综合网站 | 毛色毛片 | 久久久久国产精品美女毛片 | 久久精品精品 | 欧美一级欧美三级在线观看 | 色综合天天综合网看在线影院 | 国产在线精品一区二区夜色 | 美国三级网站 | 久久精品道一区二区三区 | 欧美成人一级片 | 欧美激情特级黄aa毛片 | 国产精品久久自在自线观看 | 亚洲热播 | 亚洲综合综合在线 | 欧美日本一区二区 | 国产精品美女一级在线观看 | 欧美成人免费xxx大片 | 国产精品久久久久免费视频 | 亚洲www| 国产欧美va欧美va香蕉在线观 | 一级毛片q片 | 毛片免费观看日本中文 | 国产成人精品一区二三区在线观看 | 国产精品外围在线观看 | 亚洲精品中文字幕在线 | 男人的天堂久久 | 久久高清一区二区三区 | 99久久伊人一区二区yy5099 | 国产在线播放一区 | 久久福利网站 | 国产成人在线视频网站 | 九九视频免费在线观看 | 欧美日韩在线观看区一二 | 亚洲欧美94色 | 她也啪在线视频精品网站 | 亚洲一区欧美 | 91久久| 久久精品国产精品亚洲20 | 日本一线a视频免费观看 |