子组件向父组件传值:子组件通过$.emit()方法以事件形式向父组件发送消息传值;

使用步骤:

定义组件:现有自定义组件com-a、com-b,com-a是com-b的父组件;

准备获取数据:父组件com-a要获取子组件data中的height属性;

在子组件com-b中,需要用$.emit()方法将数据以事件的形式发送,$.emit('sendData', data, data…),红色的部分事件名可自定义,数据可传递多个;

在父组件中使用子组件的地方 <com-b @自定义事件名='getData'></com-b> 监听子组件自定义的事件,并且在方法中获取数据;

在父组件data定义height属性; 在父组件中实现getData(height)方法,方法参数是子组件传递的数据,例如这里直有一个height,然后为this.height赋值; 赋值完毕后就可以使用了;

初始时在子组件中定义width和height

通过一个button来进行实现子组件向父组件进行传值

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>子组件向父组件传值</title>
<script type="text/javascript" src="../js/vue.js" ></script>
</head>
<body>
<div>
<father-component ></father-component>
</div>
</body>
<template id="father-template">
<div>
<h2> 父组件</h2>
myData:<span>{{name}},
{{id}},
{{user.username}},{{age}}
</span><br />
childData:<span> width:{{width}}</span><br /><span>height:{{height}}</span> <hr />
<child-component :id='id' :age='age' @sent-event='getData'></child-component>
</div>
</template>
<template id="child-template">
<div>
<p> 子组件</p>
fatherData:<span >{{name}},{{id}},{{user.username}},{{age}}</span><br />
myData:<span> width:{{width}}</span><br /><span>height:{{height}}</span><br />
<button @click="setData">向父组件传送数据</button> </div>
</template>
<script> new Vue({ data:{ }, components:{
"father-component":{ methods:{ getData(width,height){
this.width=width;
this.height=height;
}
},
data(){
return{
id:"",
name:'perfect', user:{ username:'博客园---perfect*',
password:'' },
age:,
width:,
height:
}
}, template:'#father-template', components:{
"child-component":{ methods:{
setData(){
console.log(this);//这里的this指的是child-component实例
this.$emit('sent-event',this.width,this.height);
}
},
data(){ return{
width:,
height:
}
}, template:'#child-template', props:{ name:{
type:String,
//required:true,//必须进行传值 default:'perfect*'//定义一个默认值
},
id:[Number,String],
user:{
type:Object,
default:function(){
return {username:'perfect***',password:''}
}
}, age:{
type:Number,
validator: function (value) {
return value>=;
} }
}
}
}, }
} }).$mount('div');
</script>
</html>

Vue 组件&组件之间的通信 之 子组件向父组件传值的更多相关文章

  1. layui type:2 iframe子页面向父页面传值

    需求: 选择子页面表格中的radio或者双击该行,得到的该行数据传到父页面,由父页面渲染. 网上的各种方法都用了,父页面就是获取不到子页面传的值,过了一晚上,睡了一觉,柳暗花明又一村. layui t ...

  2. vue子组件的样式没有加scoped属性会影响父组件的样式

    scoped是一个vue的指令,用来控制组件的样式生效区域,加上scoped,样式只在当前组件内生效,不加scoped,这个节点下的样式会全局生效. 需要注意的是:一个组件的样式肯定是用来美化自己组件 ...

  3. vue组件样式添加scoped属性之后,无法被父组件修改。或者无法在本组件修改element UI样式

    在vue开发中,需要使用scoped属性避免样式的全局干扰,但是这样在父组件中是无法被修改的,不仅如此如果项目中用了UI框架比如element Ui,这个时候在本组件也无法修改样式,因为权重问题.但是 ...

  4. vue 子页面,向父页面 传值...

    子组件 通过 事件 向父组件传值..... 父组件 方法: methods: { appendData: function (list) { console.log(list); for (var i ...

  5. vue组件父子之间相互通信案例

  6. Vue.js如何获得兄弟元素,子元素,父元素(DOM操作)

    我不是代码的生产者,我只是知识的搬运工. 戳这

  7. WinFrom子窗体向父窗体传值

    父窗框mainForm;子窗体childForm,利用事件进行传值 在子窗体中的操作: public event EventHandler accept;public string value; pr ...

  8. MVC弹出子页面向父页面传值

    实现思路是使用js在父子页面间传值 视图一代码,父页面 @{ ViewBag.Title = "Index"; } <script type="text/javas ...

  9. layui 子页面向父页面传值

    实现功能:单击确定按钮将选中的id传到父页面并关闭当前子页面. 首先在父页面定义一个函数: //分配产品 function ChooseAdidValues(v) { if (v != "& ...

随机推荐

  1. Differenciate or distinguish between outlook attachment and embedded image/signature using property accessor in C#.NET

    These days, outlook emails are composed in three formats; plain text, html and rtf (rich text format ...

  2. L1-016 查验身份证 (15 分)【考细心,考flag设置】

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...

  3. Visual Studio Code for mac 设置中文

    1,mac系统VScode设置中文 macOS 快捷键:command + shift + p 输入搜索 configure language       1.Ctrl+Shift+P 打开命令 2. ...

  4. Kali 开启 SSH 服务方法

    尝试了开启kali的ssh,方法如下: 1.修改sshd_config文件.命令:vim /etc/ssh/sshd_config 2.将#PasswordAuthentication no的注释去掉 ...

  5. char和QChar(Unicode的编码与内存里的值还不是一回事)

    char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’;那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111,8 ...

  6. numpy(四)

    逻辑符 : ==  !=  <  > <=  >= x=np.array([1,3,5]) x<3 array([True,False,,False]) (2*x) == ...

  7. DateTimeFormat

    中文:星期一,星期二 System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.Da ...

  8. Mysql笔试题

    1.查询Student表中的所有记录的Sname.Ssex和Class列. SELECT Sname,Ssex,Class FROM Students; 2.查询教师所有的单位即不重复的Depart列 ...

  9. python函数带()与否

    一.不带括号时,调用的是这个函数本身 ,是整个函数体,是一个函数对象,不须等该函数执行完成二.带括号(参数或者无参),调用的是函数的执行结果,须等该函数执行完成的结果 进程和线程的target=fun ...

  10. JavaScript实现RSA加解密

    在GitHub上找到jsencrypt.js对RSA加解密的工具文件,地址分别是:https://github.com/travist/jsencrypt和https://github.com/ope ...