vue子組件如何使用父組件傳過(guò)來(lái)的值
子組件使用父組件傳過(guò)來(lái)的值
父組件
<alarmstatistics :roless.sync="role"></alarmstatistics> ??import alarmstatistics from "./alarmstatistics.vue"; ? components: { ? ? alarmstatistics, ? },
子組件
? props: ["roless"], ?? ? data() { ? ? return { ? ? ? role:this.roless, ? }, ?? ? mounted() { ? ? if(this.role==3){ ? ? ? this.chartY = "9.5%"; ? ? }else{ ? ? ? this.chartY = "18%"; ? ? } ? },
如果是使用父組件接口返回來(lái)的值,在html中直接使用
? props: ["infoDatac"], ? ? ? <li ? ? ? ? v-for="(item,i) in infoDatac.notice.admitted" ? ? ? ? :key="'A'+ i" ? ? ? > ? ? ? ? <div>申請(qǐng)單號(hào):{{ item.applyCode }}</div> ? ? ? ? <div>使用時(shí)間:{{ item.useTime }}</div> ? ? ? ? <span>{{ item.title }}</span> ? ? ? </li>
vue子組件調(diào)用父組件數(shù)據(jù)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="" id="myVue"> <my-component> </my-component> </div> <!--子組件--> <template id="child" > <div id=""> <div @click='changedata'>子組件:{{data}}</div> </div> </template> <!--父組件--> <template id="father"> <div> <mycomponent-child v-bind:data="str"></mycomponent-child> </div> </template> </body> <script type="text/javascript" charset="utf-8"> /*在父組件中的數(shù)據(jù)str, * 將父組件的數(shù)據(jù)綁定到子組件的屬性data上 * 然后在子組件中就可以通過(guò)props接收到, * 這樣在子組件中就可以使用變量 this.data1訪問(wèn)到 父組件的 str1對(duì)應(yīng)的值了。 */ //當(dāng)點(diǎn)擊子組件,觸發(fā)子組件的changedata方法,通過(guò)this.data = "父組件值被子組件修改了";改變了父級(jí)的str的值 //通過(guò) this.$parent.fn()訪問(wèn)父組件的方法fn()。 var child={ props:["data"], template:"#child", data:function(){ return{ str:"我是子組件數(shù)據(jù)" } }, methods:{ changedata:function(){ this.data = "父組件值被子組件修改了"; this.$parent.fn(); } } } /*父組件*/ var father={ template:"#father", data:function(){ return{ str:"我是父組件數(shù)據(jù)" } }, methods:{ fn:function(){ alert("我是父組件方法") } }, components:{ "mycomponentChild":child } } vm=new Vue({ //el:"#myVue", components:{ "myComponent":father } }).$mount('#myVue'); </script> </html>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue中的父子傳值雙向綁定及數(shù)據(jù)更新問(wèn)題
這篇文章主要介紹了vue中的父子傳值雙向綁定及數(shù)據(jù)更新問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06vue實(shí)現(xiàn)簡(jiǎn)單的星級(jí)評(píng)分組件源碼
這篇文章主要介紹了vue星級(jí)評(píng)分組件源碼,代碼簡(jiǎn)單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11vue element 中的table動(dòng)態(tài)渲染實(shí)現(xiàn)(動(dòng)態(tài)表頭)
這篇文章主要介紹了vue element 中的table動(dòng)態(tài)渲染實(shí)現(xiàn)(動(dòng)態(tài)表頭),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11vue實(shí)現(xiàn)購(gòu)物車(chē)結(jié)算功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車(chē)結(jié)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問(wèn)題
這篇文章主要介紹了vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05vue中的事件觸發(fā)(emit)及監(jiān)聽(tīng)(on)問(wèn)題
這篇文章主要介紹了vue中的事件觸發(fā)(emit)及監(jiān)聽(tīng)(on)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)2
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)的相關(guān)資料,實(shí)現(xiàn)Observer構(gòu)造函數(shù),監(jiān)聽(tīng)已有數(shù)據(jù)data中的所有屬性,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01vue中provide和inject的用法及說(shuō)明(vue組件爺孫傳值)
這篇文章主要介紹了vue中provide和inject的用法及說(shuō)明(vue組件爺孫傳值),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05