vue子組件如何使用父組件傳過來的值
更新時間:2022年04月08日 11:29:09 作者:我是小路路呀
這篇文章主要介紹了vue子組件如何使用父組件傳過來的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
子組件使用父組件傳過來的值
父組件
<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%"; ? ? } ? },
如果是使用父組件接口返回來的值,在html中直接使用
? props: ["infoDatac"], ? ? ? <li ? ? ? ? v-for="(item,i) in infoDatac.notice.admitted" ? ? ? ? :key="'A'+ i" ? ? ? > ? ? ? ? <div>申請單號:{{ item.applyCode }}</div> ? ? ? ? <div>使用時間:{{ 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上 * 然后在子組件中就可以通過props接收到, * 這樣在子組件中就可以使用變量 this.data1訪問到 父組件的 str1對應(yīng)的值了。 */ //當(dāng)點擊子組件,觸發(fā)子組件的changedata方法,通過this.data = "父組件值被子組件修改了";改變了父級的str的值 //通過 this.$parent.fn()訪問父組件的方法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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue中的父子傳值雙向綁定及數(shù)據(jù)更新問題
這篇文章主要介紹了vue中的父子傳值雙向綁定及數(shù)據(jù)更新問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06vue element 中的table動態(tài)渲染實現(xiàn)(動態(tài)表頭)
這篇文章主要介紹了vue element 中的table動態(tài)渲染實現(xiàn)(動態(tài)表頭),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11vue中radio單選框如何實現(xiàn)取消選中狀態(tài)問題
這篇文章主要介紹了vue中radio單選框如何實現(xiàn)取消選中狀態(tài)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05vue中的事件觸發(fā)(emit)及監(jiān)聽(on)問題
這篇文章主要介紹了vue中的事件觸發(fā)(emit)及監(jiān)聽(on)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)2
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)的相關(guān)資料,實現(xiàn)Observer構(gòu)造函數(shù),監(jiān)聽已有數(shù)據(jù)data中的所有屬性,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01vue中provide和inject的用法及說明(vue組件爺孫傳值)
這篇文章主要介紹了vue中provide和inject的用法及說明(vue組件爺孫傳值),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05