vue.js父子組件通信動態(tài)綁定的實例
更新時間:2018年09月28日 15:04:20 作者:蘭亭古墨
今天小編就為大家分享一篇vue.js父子組件通信動態(tài)綁定的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
<!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 給父組件發(fā)送消息 Vue.component('child', { props: ['myMessage'],//這里的props選項是用來實現父子組件的通信的,傳遞下來的消息字組件用花括號接住 template: '#child' }); new Vue({ el:'#app' }) </script> </html>
以上這篇vue.js父子組件通信動態(tài)綁定的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于Vue3+TypeScript的全局對象的注入和使用詳解
這篇文章主要介紹了基于Vue3+TypeScript的全局對象的注入和使用,本篇隨筆主要介紹一下基于Vue3+TypeScript的全局對象的注入和使用,需要的朋友可以參考下2022-09-09