vue $emit 子传父
Vue Emit
2023-09-11 14:22:19 时间
我们使用子组件传递值给父组件使用 $emit
代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> *{ padding: 0; margin: 0; } .main{ width: 200px; } .head{ width: 200px; height: 80px; background-color: purple; } .main2{ width: 200px; height: 300px; } .main2 .aside{ float: left; width: 30%; height: 100%; background-color: green; } .main2 .content{ float: left; width: 70%; height: 100%; background-color: red; } </style> </head> <body> <div id="app"></div> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> <script type="text/javascript"> Vue.component('Vbtn',{ template:` <button>按钮</button> ` }); var Vcontent = { template:` <div class='content'>我是内容组件 <ul> <li v-for = '(item,index) in posts'> <h3>{{item.title}}</h3> <h4>{{item.content}}</h4> </li> </ul> </div> `, props:['posts'] } var Vaside = { template:` <div class='aside'>我是侧边栏组件 </div> ` }; var Vheader = { template:` <div class='head'> 我是头部组件 <button @click = 'changeFontSize'>字体变大</button> </div> `, methods:{ changeFontSize(){ this.$emit('change') } } }; // 1.声明局部组件 App入口组件 var App = { template:` <div class='main' :style = '{fontSize:postFontSize+"em"}'> <Vheader @change = 'changeHandler'/> <div class = 'main2'> <Vaside /> <Vcontent :posts = 'posts'/> </div> </div> `, methods:{ changeHandler(){ this.postFontSize+=.1; } }, data(){ return { posts:[ {id:1,title:"我的第一个学习框架",content:'vue'}, {id:2,title:"我的第二个学习框架",content:'react'}, {id:3,title:"我的第三个学习框架",content:'angular'} ], postFontSize:1 } }, components:{ Vheader, Vaside, Vcontent } }; new Vue({ el:'#app', // 3.使用 template:'<App></App>', data(){ return { } }, // 2.挂载组件 components:{ App } }); </script> </body> </html>
效果:
分析:
相关文章
- [转] vue自定义组件(通过Vue.use()来使用)即install的使用
- Vue——vue中的双向数据绑定
- vue-devtools 获取到 vuex store 和 Vue 实例的?
- Vue vue-awesome-swiper 的坑
- Vue绑定事件,双向数据绑定,只是循环没那么简单
- Vue 爬坑之路(一)—— 使用 vue-cli 搭建项目 (增补)
- Vue 生命周期 详细介绍(面试必考,内附实例截图)
- Vue 第三方库的使用
- swiper在vue项目中的循环轮播bug以及点击事件
- vue-router编程式跳转
- 解决webstorm中vue语法没有提示
- vue学习笔记九:Jquery VS Vue之遍历方法对照
- vue学习笔记四:Jquery VS Vue之元素操作明细对照
- 浅谈vue中插件的使用方法Vue.use(xxx),原理及实现
- i18n在vue项目中使用
- 图片-滑动-解锁-组件-vue-canvas
- vue问题解决:Vue packages version mismatch 版本始终不对的解决
- Vue子组件向父组件传值(this.$emit()方法)
- Django+Vue项目学习第七篇:利用nginx解决跨域问题
- Django+Vue项目学习第六篇:vue+django发送post请求,设置不同content-type,前后端如何处理参数
- Django+Vue项目学习第二篇:vue项目创建