vue組件父與子通信詳解(一)
本文實例為大家分享了vue組件父與子通信的具體代碼,供大家參考,具體內(nèi)容如下
一、組件間通信(父組件 --> 子組件)
步驟:
①父組件在調(diào)用子組件 傳值
<child-component myValue="123"> </child-component>
②在子組件中 獲取父組件傳來的值
Vue.component('child-component',{ props:['myValue'], template:'' })
代碼1:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>父傳子</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <parent-component></parent-component> </div> <script> // 在vue中一切都是組件 //父傳子 Vue.component("parent-component",{ data:function(){ return { gift:"傳家寶" } }, template:` <div> <h1>這是父組件</h1> <hr> <child-component v-bind:myValue="gift"></child-component> </div> ` }) Vue.component("child-component",{ props:["myValue"], template:` <div> <h1>這是子組件</h1> <p>{{"父傳遞的值:"+myValue}}</p> </div> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
myValue是屬性名,必須都一樣……拿data中的用v-bind:或者:
props是property屬性,是個數(shù)組
代碼2:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>父子之間通信練習</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <my-login></my-login> </div> <script> /* 登錄窗口 創(chuàng)建4個組件,分別是my-label my-input my-button my-login(復合組件) */ Vue.component("my-label",{ props:["myLabel"], template:` <div> <label>{{myLabel}}</label> </div> ` }) Vue.component("my-input",{ template:` <div> <input type="text"/> </div> ` }) Vue.component("my-button",{ props:["myButton"], template:` <div> <button>{{myButton}}</button> </div> ` }) //復合組件 Vue.component("my-login",{ data:function(){ return { uname:"用戶名", upwd:"密碼", login:"登錄", register:"注冊" } }, template:` <div> <my-label v-bind:myLabel="uname"></my-label> <my-input></my-input> <my-label v-bind:myLabel="upwd"></my-label> <my-input></my-input> <my-button v-bind:myButton="login"></my-button> <my-button v-bind:myButton="register"></my-button> </div> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
代碼3:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <script src="js/vue.js"></script> <title></title> </head> <body> <div id="container"> <my-login></my-login> </div> <script> Vue.component('my-label',{ props:['labelName'], template:'<label>{{labelName}}</label>' }) Vue.component('my-input',{ props:['tips'], template:'<input type="text" :placeholder="tips"/>' }) Vue.component('my-button',{ props:['btnName'], template:'<button>{{btnName}}</button>' }) Vue.component('my-login',{ template:` <form> <my-label labelName="用戶名"></my-label> <my-input tips="請輸入用戶名"></my-input> <br/> <my-label labelName="密碼"></my-label> <my-input tips="請輸入密碼"></my-input> <br/> <my-button btnName="登錄"></my-button> <my-button btnName="注冊"></my-button> </form> ` }) new Vue({ el: '#container', data: { msg: 'Hello Vue' } }) </script> </body> </html>
要拿到data中的數(shù)據(jù)就要v-bind,否則就不用。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
element的el-tree多選樹(復選框)父子節(jié)點關聯(lián)不關聯(lián)
最近想要實現(xiàn)多選框關聯(lián)的功能,但是卻出現(xiàn)了element的el-tree多選樹(復選框)父子節(jié)點關聯(lián)不關聯(lián)的問題,本文就來介紹一下解決方法,一起來了解一下2021-05-05Vue手把手教你擼一個 beforeEnter 鉤子函數(shù)
這篇文章主要介紹了Vue手把手教你擼一個 beforeEnter 鉤子函數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法
這篇文章主要給大家介紹了關于Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-02-02用Vue.js方法創(chuàng)建模板并使用多個模板合成
在本篇文章中小編給大家分享了關于如何使用Vue.js方法創(chuàng)建模板并使用多個模板合成的相關知識點內(nèi)容,需要的朋友們學習下。2019-06-06