国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue通過ref父子組件拿值方法

2024-05-06 15:30:13
字體:
來源:轉載
供稿:網(wǎng)友

父拿子的值

<!doctype html><html><head> <meta charset="UTF-8"> <script src="https://cdn.bootcss.com/vue/2.3.2/vue.min.js"></script></head><body><div id="box"> <v-tpl1></v-tpl1></div><template id="tpl1"> <div>  <p>{{msg1}}</p>  <!--觸發(fā)拿子組件值的函數(shù)-->  <button @click="getChild">父拿子的值</button>  <!--通過ref綁定子組件的值-->  <v-tpl2 ref="shit"></v-tpl2> </div></template><template id="tpl2"> <p>{{msg2}}</p></template><script> new Vue({  el: '#box',  components: {   'v-tpl1': {    template: '#tpl1',    data(){     return {      msg1: 'msg1'     }    },    methods: {     //父組件定義一個方法通過refs拿到子組件的值     getChild(){      console.log(this.$refs.shit.msg2)     }    },    components: {     'v-tpl2': {      template: '#tpl2',      data(){       return {        msg2: 'msg2'       }      }     }    }   }  } })</script></body></html>

子拿父的值

<!doctype html><html><head> <meta charset="UTF-8"> <script src="https://cdn.bootcss.com/vue/2.3.2/vue.min.js"></script></head><body><div id="box"> <v-tpl1></v-tpl1></div><template id="tpl1"> <div>  <v-tpl2></v-tpl2> </div></template><template id="tpl2"> <div>  <button @click="getParent">子拿父的值</button>  <p>{{msg2}}</p> </div></template><script> new Vue({  el: '#box',  components: {   'v-tpl1': {    template: '#tpl1',    data(){     return {      msg1: 'msg1'     }    },    components: {     'v-tpl2': {      template: '#tpl2',      data(){       return {        msg2: 'msg2'       }      },      methods:{       getParent(){        console.log(this.$parent.msg1)       }      }     }    }   }  } })</script></body></html>

有了ref拿值不能更方便~

以上這篇Vue通過ref父子組件拿值方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 台北市| 旬邑县| 堆龙德庆县| 安达市| 西充县| 贡觉县| 中卫市| 丰顺县| 蒲城县| 昌江| 绥中县| 四平市| 龙江县| 渑池县| 中卫市| 广州市| 龙江县| 三亚市| 南漳县| 柳林县| 呼图壁县| 桂平市| 海宁市| 武汉市| 车致| 禹城市| 观塘区| 吕梁市| 靖远县| 金华市| 鹤岗市| 银川市| 丹寨县| 平塘县| 汝阳县| 吉安市| 大埔县| 台北市| 古交市| 黑水县| 尚义县|