Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn)
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)使用計(jì)算屬性
<!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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vuex拿到state中數(shù)據(jù)的3種方式與實(shí)例剖析
store是一個(gè)狀態(tài)管理工具(vueX中只有唯一 一個(gè)store),下面這篇文章主要給大家介紹了關(guān)于Vuex拿到state中數(shù)據(jù)的3種方式與實(shí)例剖析的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Element?Plus在el-form-item中設(shè)置justify-content無效的解決方案
這篇文章主要介紹了Element?Plus在el-form-item中設(shè)置justify-content無效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue中img的src是動態(tài)渲染時(shí)不顯示的解決
今天小編就為大家分享一篇Vue中img的src是動態(tài)渲染時(shí)不顯示的解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue登錄頁實(shí)現(xiàn)使用cookie記住7天密碼功能的方法
這篇文章主要介紹了vue登錄頁實(shí)現(xiàn)使用cookie記住7天密碼功能的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Vue.js組件tree實(shí)現(xiàn)無限級樹形菜單
這篇文章主要為大家詳細(xì)介紹了Vue.js組件tree實(shí)現(xiàn)無限級樹形菜單代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12vue用復(fù)選框?qū)崿F(xiàn)組件且支持單選和多選操作方式
這篇文章主要介紹了vue用復(fù)選框?qū)崿F(xiàn)組件且支持單選和多選操作方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04