vue組件父與子通信詳解(一)
本文實(shí)例為大家分享了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>父?jìng)髯?lt;/title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <parent-component></parent-component> </div> <script> // 在vue中一切都是組件 //父?jìng)髯? 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>{{"父?jìng)鬟f的值:"+myValue}}</p> </div> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
myValue是屬性名,必須都一樣……拿data中的用v-bind:或者:
props是property屬性,是個(gè)數(shù)組
代碼2:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>父子之間通信練習(xí)</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <my-login></my-login> </div> <script> /* 登錄窗口 創(chuàng)建4個(gè)組件,分別是my-label my-input my-button my-login(復(fù)合組件) */ 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> ` }) //復(fù)合組件 Vue.component("my-login",{ data:function(){ return { uname:"用戶名", upwd:"密碼", login:"登錄", register:"注冊(cè)" } }, 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="請(qǐng)輸入用戶名"></my-input> <br/> <my-label labelName="密碼"></my-label> <my-input tips="請(qǐng)輸入密碼"></my-input> <br/> <my-button btnName="登錄"></my-button> <my-button btnName="注冊(cè)"></my-button> </form> ` }) new Vue({ el: '#container', data: { msg: 'Hello Vue' } }) </script> </body> </html>
要拿到data中的數(shù)據(jù)就要v-bind,否則就不用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element的el-tree多選樹(復(fù)選框)父子節(jié)點(diǎn)關(guān)聯(lián)不關(guān)聯(lián)
最近想要實(shí)現(xiàn)多選框關(guān)聯(lián)的功能,但是卻出現(xiàn)了element的el-tree多選樹(復(fù)選框)父子節(jié)點(diǎn)關(guān)聯(lián)不關(guān)聯(lián)的問題,本文就來介紹一下解決方法,一起來了解一下2021-05-05Vue手把手教你擼一個(gè) beforeEnter 鉤子函數(shù)
這篇文章主要介紹了Vue手把手教你擼一個(gè) beforeEnter 鉤子函數(shù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Vue動(dòng)態(tài)生成el-checkbox點(diǎn)擊無法賦值的解決方法
這篇文章主要給大家介紹了關(guān)于Vue動(dòng)態(tài)生成el-checkbox點(diǎn)擊無法賦值的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02用Vue.js方法創(chuàng)建模板并使用多個(gè)模板合成
在本篇文章中小編給大家分享了關(guān)于如何使用Vue.js方法創(chuàng)建模板并使用多個(gè)模板合成的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-06-06詳解vue 計(jì)算屬性與方法跟偵聽器區(qū)別(面試考點(diǎn))
這篇文章主要介紹了詳解vue 計(jì)算屬性與方法跟偵聽器區(qū)別(面試考點(diǎn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04vue 2.1.3 實(shí)時(shí)顯示當(dāng)前時(shí)間,每秒更新的方法
今天小編就為大家分享一篇vue 2.1.3 實(shí)時(shí)顯示當(dāng)前時(shí)間,每秒更新的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09