Vue2.0使用props传递数据【methods篇】
来源:互联网 发布:知乎live500场 百度云 编辑:程序博客网 时间:2024/06/09 23:49
<body> <div id = 'app'> <div> <h3>{{title}}</h3> <p>以下为组件内容:</p> </div> <hr> <component name = 'component' :onclick = 'm_submit'></component> </div> <template id = 'component'> <div> <h3>{{component_title}}</h3> <div> <p><button @click = 'm_click'>点击</button></p> </div> <div> <p>{{name}}</p> </div> </div> </template> <script src="https://cdn.bootcss.com/vue/2.5.8/vue.min.js"></script> <script type="text/javascript"> var COMPONENT = { template : "#component", // 用props把数据传给子组件 props : { name : String, // 这个不能忘记传递【通过整个媒介把子组件对象传递给了根元素】 onclick : Function }, data : function(){ return { component_title : '组件名', } }, methods : { m_click : function() { var _this = this ; console.log('触发了组件里的点击事件') ; console.log('异步函数外的this:', this) ; setTimeout(function() { console.log('准备开始执行onclick()事件了') ; console.log('异步函数里的this:', this) ; _this.onclick(); }, 3000) ; }, m_reset : function() { console.log('触发m_reset()方法了') ; } }, mounted : function() { /** * 这边要把本组件对象传递给根元素。 * 直接用Vue.set()方法就行了。 * * 思路是: * 把本组件对象通过该组件的name属性的值component传递过去, * 这样根元素data里的component属性的值就是本组件对象了, * 也就是说根元素直接拿到了本组件对象。 * 所以根元素届时可以通过data里的属性component调用本组件的m_reset方法。 * (面向对象编程) */ Vue.set(this.$root, this.name, this) ; console.log('this:', this) ; console.log('this.name:', this.name) ; console.log('this.$root:', this.$root) ; } } ; Vue.component('component', COMPONENT) ; var _vm = new Vue({ data : { title : 'Vue2.0使用props传递数据【methods篇】', // component : null // 这样写不容易理解 component : {} // 这样写就容易理解了 }, methods : { m_submit : function() { console.log('提交成功了~~~!') ; setTimeout(function(){ console.log('数据返回了~~!') ; /** * 这边要调用子组件里的方法。 * * 思路是: * 把整个子组件对象通过子组件的“name”的值component传递过来了, * 这样父组件data里的component属性的值就是整个子组件对象了, * 也就是说父组件直接拿到了整个子组件对象。 * 所以通过component调用m_reset方法了。 * (面向对象编程) */ _vm.component.m_reset() ; }, 3000) ; } } }).$mount('#app'); </script></body>
阅读全文
0 0
- Vue2.0使用props传递数据【methods篇】
- Vue2.0使用props传递数据---3
- Vue2.0使用props传递数据---4
- Vue2.0使用props传递数据---5
- Vue2.0使用props传递数据---6
- Vue2.0使用props传递数据【data篇】
- vuejs使用 Props 传递数据
- vue.js使用props传递数据
- props数据传递
- react数据传递----props
- 关于Vue.js的组件化,使用props传递数据
- 饿了么项目---6、使用Props传递数据
- 续-React数据传递-props
- vue2.0 组件之间的数据传递
- vue2.0 组件之间的数据传递
- vue2.0兄弟组件传递数据
- React.js refs 和props传递数据
- Vue.js学习笔记:props传递数据
- Revit中读写xml方法
- 【安全牛学习笔记】XSS- 键盘记录器和反射型XSS
- Reactor模式简单实现与理解
- WPF MVVM Page 页面导航实例
- [Poi2010]Antisymmetry
- Vue2.0使用props传递数据【methods篇】
- Mysql-索引-BTree类型 ” 的sql优化
- MySQL索引使用方法和性能优化
- 高通camera基本代码架构
- Java Web报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Git学习
- 缓冲技术之五:缓冲池的LRU管理策略C++实现
- Windows 7
- Filebench的安装与使用[转载]