亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

vue 組件基礎知識總結

瀏覽:4日期:2022-10-08 14:17:29
組件基礎1 組件的復用

組件是可復用的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>

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

<!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 向子組件傳遞數據

<!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來傳遞數據。我們可以使用v-bind來動態傳遞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傳遞數據的麻煩。

4 監聽子組件事件

<!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方法并傳入事件名稱來觸發一個事件。父組件可以接收該事件。

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

<!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 組件基礎知識總結的詳細內容,更多關于vue 組件的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 一级毛片看真人在线视频 | 91免费在线视频观看 | 国产一级大片免费看 | 精品国内一区二区三区免费视频 | 国产女乱淫真高清免费视频 | 久久青草精品免费资源站 | 伊人久久精品成人网 | 国产色综合天天综合网 | 国产精品亚洲一区在线播放 | 婷婷六月综合 | 亚洲精品在线播放视频 | 中文字幕在线精品视频万部 | 国产三级视频在线观看视主播 | 国产精品一级毛片不收费 | 久久er国产精品免费观看1 | 咪咪久久| 成人人观看的免费毛片 | 欧美日韩一区二区在线视频播放 | 亚洲国产日韩综合久久精品 | 久久中文在线 | 一级黄色免费毛片 | 视色4se影院在线播放 | 丁香亚洲| 日韩一级一欧美一级国产 | 九九热视频在线观看 | 夜色成人免费观看 | 成人免费精品视频 | 日韩视频高清 | 黄色在线视频网址 | 国产黄频在线观看高清免费 | 黄短视频在线观看免费版 | 亚洲在线免费 | 国产成人18黄网站麻豆 | 国产三级做爰在线观看视频 | 国产农村妇女成人精品 | 最新国产精品视频 | 午夜看黄| 在线久综合色手机在线播放 | 久久东京伊人一本到鬼色 | 最新精品在线 | 国产精品黄大片观看 |