Vue props 单向数据流的实现
2019/6/26 23:22:31
本文主要是介绍Vue props 单向数据流的实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、props通信
注意:DOM模板的驼峰命名props要转为短横分割命名。
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div id="app"> <my-component message='来自父组件的数据' 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>
传递动态数据(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: '' }//前端全栈开发交流群: })//866109638 </script>//帮助1-3年工作人员 </body>//突破技术瓶颈,提升思维能力 </html>
2、单向数据流
(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>//前端全栈交流学习圈: //866109386,帮助1-3年前端人员 <body>//突破技术瓶颈,提升思维能力 <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">组件内容</div>', props: ['width'], computed: { style: function() { return { width: this.width + 'px' } } } }) new Vue({ el: "#app" }) </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于Vue props 单向数据流的实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04package.json 文件位置在哪?-icode9专业技术文章分享
- 2024-10-01Craco.js学习:从入门到实践指南
- 2024-10-01Create-React-App学习:入门与实践指南
- 2024-10-01CSS-in-JS学习:从入门到实践指南
- 2024-09-30JSX语法学习:从入门到初步掌握
- 2024-09-30Mock.js学习:入门教程与实战演练
- 2024-09-30React Hooks学习:从入门到实践
- 2024-09-30受控组件学习:React中的基础入门教程
- 2024-09-29JS定时器教程:初学者必看指南
- 2024-09-29JS对象教程:初学者的全面指南