1、props通信
注意:DOM模板的駝峰命名props要轉(zhuǎn)為短橫分割命名。
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div id="app"> <my-component message='來自父組件的數(shù)據(jù)' warning-text="提示信息"></my-component> </div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script type="text/javascript"> Vue.component('my-component', { template: '<div>{{warningText}}:{{message}}</div>', props: ['message', 'warningText'] }) new Vue({ el: "#app" }) </script> </body> </html>
傳遞動態(tài)數(shù)據(jù)(v-bind):
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div id="app"> <input type="text" v-model="parentMessage" /> <my-component :message='parentMessage'></my-component> </div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script type="text/javascript"> Vue.component('my-component', { template: '<div>{{message}}</div>', props: ['message'] }) new Vue({ el: "#app", data: { parentMessage: '' }//前端全棧開發(fā)交流群: })//866109638 </script>//幫助1-3年工作人員 </body>//突破技術(shù)瓶頸,提升思維能力 </html>
2、單向數(shù)據(jù)流
(1)子組件保存父組件傳遞過來的值,在子組件自己的作用域下修改和使用。
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div id="app"> <my-component :init-count='1'></my-component> </div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script type="text/javascript"> Vue.component('my-component', { template: '<div>{{initCount}}</div>', props: ['initCount'], data: function() { return this.initCount } }) new Vue({ el: "#app" }) </script> </body> </html>
(2)使用計算屬性
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head>//前端全棧交流學(xué)習(xí)圈: //866109386,幫助1-3年前端人員 <body>//突破技術(shù)瓶頸,提升思維能力 <div id="app"> <my-component :width='100'></my-component> </div> <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script> <script type="text/javascript"> Vue.component('my-component', { template: '<div :style="style">組件內(nèi)容</div>', props: ['width'], computed: { style: function() { return { width: this.width + 'px' } } } }) new Vue({ el: "#app" }) </script> </body> </html>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com