vue $emit 子传父
我们使用子组件传递值给父组件使用 $emit
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
padding: ;
margin: ;
}
.main{
width: 200px;
}
.head{
width: 200px;
height: 80px;
background-color: purple; }
.main2{
width: 200px;
height: 300px;
}
.main2 .aside{
float: left;
width: %;
height: %;
background-color: green;
}
.main2 .content{
float: left;
width: %;
height: %;
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+=.;
}
},
data(){
return {
posts:[
{id:,title:"我的第一个学习框架",content:'vue'},
{id:,title:"我的第二个学习框架",content:'react'},
{id:,title:"我的第三个学习框架",content:'angular'} ],
postFontSize: }
},
components:{
Vheader,
Vaside,
Vcontent
}
}; new Vue({
el:'#app',
// 3.使用
template:'<App></App>',
data(){
return { }
},
// 2.挂载组件
components:{
App
}
}); </script> </body>
</html>
效果:

分析:

vue $emit 子传父的更多相关文章
- vue 组件之间相互传值 父传子 子传父
1.父传子 把要传入的值放到父标签里 子组件使用props接收 父写法 子写法 2.子传父 子组件: childrenOnclick() { // 发布自定义事件 this.$emit(" ...
- Vue父子组件传值 | 父传子 | 子传父
父传子 父容器 <template> <div> <zdy :module='test'></zdy> </div> </templa ...
- Vue ---- 组价 组件化 子传父 父传子
目录 补充js的for循环: 组件 1.组件的分类: 2.组件的特点 3.创建局部组件 4.全局组件 二.组件化 一.组件传参父传子 二.组件传参:子传父 补充js的for循环: // for in遍 ...
- vue的组件通讯 父传子 -- 子传父-- 兄弟组件的传值 vue的组件传值
首先文字简单撸一下 父子传子 -------首先在父组件上绑定一个属性,在子组件里用props接收,可以是数组或者是对象 子传父 ------在父组件升上自定义一个方法,在子组件里通过this ...
- vue子传父、父传子
子传父 vue子传父使用$emit传值 子组件: <template> <div> <button @click="toParent">点击传到 ...
- Vue-组件传值:子传父和兄弟组件间常见的传值方式
前言 上篇介绍了我对vue组件化的理解和父组件对子组件传值的方式,这篇介绍下常见的子传父和兄弟组件间的传值方式 目录 子组件向父组件传值 任意组件间的传值方式 正文 子组件向父组件传值 关键知识点:$ ...
- vue传参子传父
vue子传父用$emit实现 1.文件目录结构 2.parent父组件内容 <template> <div class="wrap"> <div> ...
- Blazor和Vue对比学习(基础1.4):事件和子传父
Blazor和Vue的组件事件,都使用事件订阅者模式.相对于上一章的组件属性,需要多绕一个弯,无论Blazor还是Vue,都是入门的第一个难点.要突破这个难点,一是要熟悉事件订阅模式<其实不难& ...
- vue组件之间互相传值:父传子,子传父
今看到一篇很不错的vue组件传值文章,便于理解,遂做笔记- 一般页面的视图App.vue应为这样 一.父组件向子组件传值 1.创建子组件,在src/components/文件夹下新建一个Child.v ...
随机推荐
- MES training
unique identity 1.project name , namespace 2. select XML (not html) 3. view and controller will be i ...
- 1、eureka注册中心单机
Spring Cloud 2.x系列之 eureka注册中心单机 一.简介 Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块.而Spring Clo ...
- adb 提示adb server version(31) doesn't match this client(40) 解决办法
有时候我们用adb工具去连接安卓设备,或者模拟器的时候,会提示adb server version(31) doesn't match this client(40)这样的提示.如图 提示的字面意思就 ...
- 欧拉路径 && 欧拉回路
- 笔记62 Spring Boot快速入门(二)
SpringBoot部署 一.jar方式 1.首先安装maven. <1>下载最新的maven版本:https://maven.apache.org/download.cgi <2& ...
- 3、Python 基础类型 -- List 列表类型
2.更新列表:list.append() 3.删除列表元素 del
- fastDFS配置文件 fdfs_client.conf
# connect timeout in seconds# default value is 30sconnect_timeout=30 # network timeout in seconds# d ...
- jsp中提供的四种属性范围
参考:http://www.cnblogs.com/xdp-gacl/p/3781056.html 1.当前页:一个属性只能在一个页面中取得,跳转到其他页面无法取得 2.一次服务器请求:一个页面中设置 ...
- bzoj 3207 可持久化线段树+hash
这道题要看出来这个做法还是比较容易说一下细节 1.因为要用hash的区间值域来建树,而hash为了不冲突要开的很大,所以值域就会比较的大,不过这道题好的一点是没有修改,所以直接离散一下就会小很多 2. ...
- sql 数据库
在关系数据库中,最常用的操作就是查询.直线电机推杆 准备数据 为了便于讲解和练习,我们先准备好了一个students表和一个classes表,它们的结构和数据如下: students表存储了学生信息: ...