色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

Vue中父子組件的值傳遞與方法傳遞

瀏覽:122日期:2022-11-17 18:18:54

一.Vue中父組件向子組件傳值

利用v-bind向子組件傳值,子組件中利用props接受

<div id='app'> <father></father></div><template id='father'> <div> <!--注意點: 組件是可以使用自己的數據的--> <p>{{name}}</p> <p>{{age}}</p> <!--這里將父組件的name通過parentname傳遞給了子組件--> <son :parentname='name' :abc='age'></son> </div></template><template id='son'> <div> <!--這里通過parentname使用了父組件傳遞過來的數據--> <p>{{parentname}}</p> <p>{{abc}}</p> </div></template><script> // 父組件 Vue.component('father', { template: '#father', data: function(){ return { name: 'wqd', age: 21 } }, // 子組件 components: { 'son': {template: '#son',//這里通過parentname接收了父組件傳遞過來的數據props: ['parentname', 'abc'] } } }); // 這里就是MVVM中的View Model let vue = new Vue({ el: ’#app’, // 這里就是MVVM中的Model data: { }, });</script>

二.Vue中父組件向子組件傳遞方法

父組件利用v-on傳值,子組件this.$emit來接收

<div id='app'> <father></father></div><template id='father'> <div> <button @click='say'>我是按鈕</button> <!--這里通過parentsay將父組件的say方法傳遞給了子組件--> <son @parentsay='say'></son> </div></template><template id='son'> <div> <button @click='sonFn'>我是按鈕</button> </div></template><script> // 父組件 Vue.component('father', { template: '#father', methods: { say(){console.log('helloworld') } }, // 子組件 components: { 'son': {template: '#son',/*注意點: 和傳遞數據不同, 如果傳遞的是方法, 那么在子組件中不需要接收,需要在子組件自定義的方法中通this.$emit('自定義接收的名稱')的方法來觸發父組件傳遞過來的方法*/// props: ['parentsay']methods: { sonFn(){ this.$emit('parentsay'); }} } } }); // 這里就是MVVM中的View Model let vue = new Vue({ el: ’#app’, // 這里就是MVVM中的Model data: { }, });</script>

三.Vue中子組件向父組件傳值

this.$emit中第一個參數為接收父組件傳遞的方法,第二個參數即為向父組件傳遞的值

<div id='app'> <father></father></div><template id='father'> <div> <button @click='say'>我是按鈕</button> <!--這里通過parentsay將父組件的say方法傳遞給了子組件--> <son @parentsay='say'></son> </div></template><template id='son'> <div> <button @click='sonFn'>我是按鈕</button> </div></template><script> // 父組件 Vue.component('father', { template: '#father', methods: { //data用來接受子組件傳遞的值 say(data){console.log(data); } }, // 子組件 components: { 'son': {template: '#son',methods: { sonFn(){ // 第一個參數: 需要調用的函數名稱 // 后續的參數: 給調用的函數傳遞的參數 this.$emit('parentsay', '你好'); }} } } }); // 這里就是MVVM中的View Model let vue = new Vue({ el: ’#app’, // 這里就是MVVM中的Model data: { }, });</script>

到此這篇關于Vue中父子組件的值傳遞與方法傳遞的文章就介紹到這了,更多相關Vue父子組件傳遞內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 欧美亚洲另类在线 | 97干干干 | 久久频这里精品99香蕉久网址 | 国产激情视频在线播放 | 中文字幕在线看视频一区二区三区 | 91精品成人福利在线播放 | 国产成人影院一区二区 | 久久久9视频在线观看 | 亚洲精品视频区 | 一区二区三区高清不卡 | 九九视频在线看精品 | 亚洲精品视频网 | 欧美高清视频手机在在线 | 男女午夜爱爱久久无遮挡 | 99精品在线观看 | 亚洲国产爱久久全部精品 | 国产日产高清欧美一区二区三区 | 女人张开腿让男人操 | 亚洲理论片在线中文字幕 | 中文字幕成人网 | 日韩欧美中文字幕一区二区三区 | 亚洲人成网站在线在线 | 911精品国产亚洲日本美国韩国 | 国产的一级片 | 目韩一区二区三区系列片丶 | 久草精彩视频 | 欧美一级成人 | rion美乳弹出来四虎在线观看 | 国产手机精品a | 国产精品国产国产aⅴ | 亚洲成人中文 | 在线视频中文字幕 | 欧美色视频日本片高清在线观看 | 91av手机在线 | 久久se精品一区精品二区 | 日韩免费毛片全部不收费 | 一级毛片美国 | 亚洲在线免费免费观看视频 | 91久久精品国产91久久性色tv | 亚洲成人免费在线观看 | 成年人视频免费网站 |