這期內容當中小編將會給大家?guī)碛嘘P怎么在Vue中通過ref父子組件拿值,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注于文昌企業(yè)網(wǎng)站建設,響應式網(wǎng)站開發(fā),電子商務商城網(wǎng)站建設。文昌網(wǎng)站建設公司,為文昌等地區(qū)提供建站服務。全流程按需定制制作,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務
父拿子的值
<!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>
Vue具體輕量級框架、簡單易學、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結構的分離、虛擬DOM、運行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗。
上述就是小編為大家分享的怎么在Vue中通過ref父子組件拿值了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:怎么在Vue中通過ref父子組件拿值
文章起源:http://m.kartarina.com/article28/pgojjp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿網(wǎng)站建設、網(wǎng)站導航、自適應網(wǎng)站、關鍵詞優(yōu)化、標簽優(yōu)化、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)