如下所示:
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body><div id='app'> <!--這里的作用是將父組件渲染到頁面上--> <father></father></div></body><script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script><script type="text/x-template" id="father"> <div> <!--這里實現一個雙向綁定--> <!--parentMsg變量必須在data中聲明,不然會報錯,我就錯在這個坑--> <input v-model="parentMsg"> <br> <child :my-message="parentMsg"></child> </div></script><script type="text/x-template" id="child"> <div> {{'父組件傳遞的數據為:'+ myMessage }} </div></script><script> Vue.component('father',{// 這里我直接把template寫到前面script標簽中了,寫在這里一大坨,難看 template:'#father', data:function(){ return { parentMsg:'' } } }); //在 Vue 中,父子組件的關系可以總結為 props down, events up。 // 父組件通過 props 向下傳遞數據給子組件,子組件通過 events 給父組件發送消息 Vue.component('child', { props: ['myMessage'],//這里的props選項是用來實現父子組件的通信的,傳遞下來的消息字組件用花括號接住 template: '#child' }); new Vue({ el:'#app' })</script></html>
以上這篇vue.js父子組件通信動態綁定的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答