亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    vue組件傳值有哪五種方法

    vue組件傳值的五種方法:1、父組件向子組件進(jìn)行傳值;2、子組件向父組件進(jìn)行傳值;3、相鄰兄弟組件間進(jìn)行傳參;4、遠(yuǎn)兄弟組件間進(jìn)行傳參;5、EventBus傳參。

    vue組件傳值有哪五種方法

    本教程操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。

    Vue的通信方式,也可以說(shuō)是傳參方式:

    • 父組件向子組件進(jìn)行傳值:

    • 子組件向父組件進(jìn)行傳值:

    • 相鄰兄弟組件間進(jìn)行傳參(親兄弟)

    • 遠(yuǎn)兄弟傳參(表兄弟)

    • EventBus傳參

    一、父子傳參

    原理:父控制子,通過(guò)子組件的props屬性,拋出子組件自定義標(biāo)簽屬性,來(lái)接收父組件的操作狀態(tài)
    例子:父級(jí)里的一個(gè)按鈕,控制子組件里的一個(gè)p的顯示隱藏

    <!DOCTYPE html> <html> <head>     <meta charset="UTF-8">     <title>Document</title> </head>  <style>     .div{         width:200px;         height:200px;         background:pink;     } </style>  <body>     <!-- 這里的app范圍就是  子組件son  的父級(jí) -->     <div id="app">         <button @click='change'>父按鈕</button>         <hr>         <!-- **********  自定義標(biāo)簽屬性test,接收父級(jí)的state ************-->         <son :test='state'></son>     </div>      <template id="tp1">         <div>             <!-- ************  調(diào)用自定義屬性test **************-->             <div v-show='test'>我是子組件的div</div>         </div>     </template>      <script src="../vue/vue.js"></script>     <script>         // 局部定義  子組件son         new Vue({             el:"#app",             data:{                 state:true             },             methods:{                 change(){                     this.state = !this.state;                 }             },             components:{                 son:{                     template:"#tp1",                     //*********** 拋出自定義標(biāo)簽屬性 ***************                     props:['test']                 }             }         })     </script>  </body> </html>

    效果:

    vue組件傳值有哪五種方法

    二、子父?jìng)鲄?/h2>

    原理:子控制父,子組件綁定自定義事件,來(lái)處理父組件的方法函數(shù),通過(guò).$emit(‘自定義事件’,[參數(shù)])來(lái)觸發(fā)屬于自己的自定義事件
    例子:子組件里一個(gè)按鈕,控制父組件里的一個(gè)p的顯示隱藏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head>  <style>     .div{         width:200px;         height:200px;         background:pink;     } </style>  <body>  <div id="app">         <c1></c1>      </div>           <!-- 父組件c1   子組件c2   子組件自定義事件test-->      <template id="c1">         <div>             <div v-show='state'>father顯示/隱藏</div>             <hr>             <!--************ 子組件c2自定義事件,執(zhí)行父組件c1的方法函數(shù)change_f ***************** -->             <c2 @test='change_f'></c2>         </div>     </template>      <template id="c2">         <div>             <button @click='change_son'>子按鈕</button>         </div>     </template>  <!-- 引入Vue.js框架文檔,可在官方文檔下載--> <script src='../vue/vue.js'></script> <script>     //全局定義     // 實(shí)例化 父組件c1     Vue.component("c1",{         template:"#c1",         data(){             return {                 state:true             }         },         methods:{             change_f(){                 this.state = !this.state;             }         }     })     // 實(shí)例化 子組件c2     Vue.component("c2",{         template:"#c2",         methods:{             change_son(){                 // ************ 在子組件方法里,觸發(fā)子組件自定義事件 ******************                 this.$emit("test")             }           }     })     //實(shí)例化一個(gè)Vue對(duì)象 new Vue({         el:"#app"     }) </script> </body> </html>

    效果:

    vue組件傳值有哪五種方法

    三、相鄰兄弟傳參(親兄弟)

    原理:通過(guò)一個(gè)公有的父元素作為橋接(實(shí)例 組件),結(jié)合父子props 傳參 、子父自定義事件

    例子:c1、c2是兄弟關(guān)系 c1可用控制c2里元素的顯示隱藏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head>  <style> .div{ width:200px; height:200px; background:pink; } </style>  <body> <div id="app"> 父級(jí)狀態(tài) <p>{{state}}</p> <hr> <c1 @test_c1='change_f'></c1> <hr> <c2 :test_c2='state'></c2> </div>  <template id="c1"> <div>這里是c1組件 <button @click='change_c1'>c1按鈕</button> </div> </template>  <template id="c2"> <div>這里是c2組件,狀態(tài):{{test_c2}} <div v-show='test_c2'>我是c2中的div</div> </div> </template>  <script src='../vue/vue.js'></script> <script>  Vue.component("c2",{ template:"#c2", props:['test_c2'] })  Vue.component("c1",{ template:"#c1", methods:{ change_c1(){ this.$emit("test_c1") } } })  new Vue({ el:"#app", data:{ state:true }, methods:{ change_f(){ this.state = !this.state; } } }) </script> </body> </html>

    效果:

    vue組件傳值有哪五種方法

    四、遠(yuǎn)兄弟傳參(表兄弟)

    原理:通過(guò)創(chuàng)建一個(gè)中間實(shí)例,注冊(cè)一個(gè)事件,在被控組件中,通過(guò)事件攜帶要執(zhí)行的函數(shù),在主控組件中,通過(guò)事件,改變相應(yīng)的操作

    vue組件傳值有哪五種方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head>  <body> <div id="app"> <father></father> </div> <template id="father"> <div> 這是父組件 <hr> <son1></son1> <hr> <son2></son2> </div> </template> <template id="son1"> <div> {{name}} <button @click='click_son1'>觸發(fā)angle中間事件</button> </div> </template> <template id="son2"> <div> {{name}} </div> </template>  <script src='../vue/vue.js'></script> <script> //********** 創(chuàng)建一個(gè)angle實(shí)例,作為中間變量(全局) ************** let angel = new Vue();  new Vue({ el:"#app", components:{ father:{ template:"#father", components:{ son1:{ template:"#son1", data(){ return { name:"我是son1" }  }, methods:{ click_son1(){ // ***************  通過(guò)angel注冊(cè)的test事件,修改son2中name的值 ************ angel.$emit('test','哈哈!修改成功!') } } }, son2:{ template:"#son2", data(){ return { name:"我是son2" } }, methods:{ change(val){ this.name = val; } }, //生命周期,自動(dòng)執(zhí)行,組件準(zhǔn)備ok就可用 mounted(){ // ***************  通過(guò)angel注冊(cè)的test事件,將son1的修改方法傳過(guò)去 ************ angel.$on('test',this.change) } } } } } }) </script>  </body> </html>

    效果:

    點(diǎn)擊前:

    vue組件傳值有哪五種方法
    點(diǎn)擊后:

    vue組件傳值有哪五種方法

    五、EventBus傳參

    1.在main.js種掛載全局EventBus

    Vue.prototype.$EventBus = new Vue()

    2.A組件

    <template>     <div class="wrap">         <div>我是組件A</div>         <button @click="sendMsg">發(fā)送</button>     </div> </template>  <script>     export default {         name: "A",         methods:{             sendMsg(){                this.$EventBus.$emit('sendMsg',"這是組件A發(fā)送的消息!")             }         }     } </script>

    3.B組件

    <template>     <div>         <div>我是組件B</div>     </div> </template>  <script>     export default {         name: "B",         mounted(){             this.$EventBus.$on('sendMsg',(msg)=>{                 console.log(msg);//這是組件A發(fā)送的消息!             })         },     } </script>

    通過(guò)掛載全局Vue對(duì)象傳遞參數(shù)。

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)