本人對Vue組件間通信不懂,搜索了很多關于Vue 父子組件間通信介紹,下面我來記錄一下,有需要了解Vue 父子組件、組件間通信的朋友可參考。希望此文章對各位有所幫助。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>組件</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <script src="../js/Vue.js"></script> <script> window.onload = function(){ var vm = new Vue({ el:'#box', data:{}, components:{ aaa:{ template:'<h2>這是aaa組件<bbb></bbb></h2>', components:{ bbb:{ template:'<h2>這是bbb組件</h2>' } } } } }); } </script></head><body><div id="box"> <aaa></aaa></div></body></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/vue1.0.js"></script> <script> //子組件獲取父組件的值 window.onload = function () { var vm = new Vue({ el: '#box', data: { a: 'aaa' }, components: { 'aaa': { data(){ return { msg: 111, msg2: '這是父組件的數據' } }, template: '#aaa', components: { 'bbb': { //html中的my-msg這種形式,js中應該采用駝峰命名myMsg props: ['mmm', 'myMsg'], template: '<h3>我是bbb組件->{{mmm}}<br>{{myMsg}}</h3>' } } } } }); } </script></head><body><div id="box"> <aaa> </aaa></div><template id="aaa"> <h1>11111</h1> <bbb :mmm="msg2" :my-msg="msg"></bbb></template></body></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/vue1.0.js"></script> <script> //子組件獲取父組件的值 window.onload = function () { var vm = new Vue({ el: '#box', data: { a: 'aaa' }, components: { 'aaa': { data(){ return { msg: 111, msg2: '這是父組件的數據' } }, template: '#aaa', components: { 'bbb': { //html中的my-msg這種形式,js中應該采用駝峰命名myMsg props: ['mmm', 'myMsg'], template: '<h3>我是bbb組件->{{mmm}}<br>{{myMsg}}</h3>' } } } } }); } </script></head><body><div id="box"> <aaa> </aaa></div><template id="aaa"> <h1>11111</h1> <bbb :mmm="msg2" :my-msg="msg"></bbb></template></body></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>父組件獲取子組件的值</title> <script src="../js/vue1.0.js"></script> <script> //父組件獲取子組件的值 window.onload = function () { var vm = new Vue({ el:'#box', data:{ a:'aaa' }, components:{ 'aaa':{ data(){ return { msg:111, msg2:'我是父組件的數據' } }, template:'#aaa', methods:{ get(msg){ this.msg = msg; } }, components:{ 'bbb':{ data(){ return { a:'我是子組件的數據' } }, template:'#bbb', methods:{ send(){ this.$emit('child-msg',this.a); } } } } } } }); } </script></head><body><div id="box"> <aaa></aaa></div><template id="aaa"> <span>我是父級 -》 {{msg}}</span> <bbb @child-msg="get"></bbb></template><template id="bbb"> <h3>子組件</h3> <input type="button" value="send" @click="send" /></template></body></html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答