Element使用的是Vue2.0版本,众所周知在Vue 1.0升级到2.0中去除了$broadcast和$dispatch方法。

1、父组件向子组件传值

a、app.vue父组件

<template>
<div id="app">
<h1>{{title}}</h1>
<h1 v-text="title"></h1>
<h1 v-html="title"></h1>
<input type="text" v-model="newItem" v-on:keyup.enter="addNew">
<ul>
<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">
{{item.label}}
</li>
</ul>
<componentA msgfromfather='hongMaJu'></componentA>
<component-a></component-a>
</div>
</template> <script>
import Store from './Store'
import componentA from './components/componentA'
//console.log(Store)
export default { data () {
return {
title: '<span>?</span>this is a todolist',
items:Store.fetch(),
// items:[
// {
// label:'coding',
// isFinished:false // },
// {
// label:'walking',
// isFinished:true // }
// ],
newItem:''
}
},
components:{componentA},
watch:{
items:{
handler:function(items){
// console.log(val,oldVal)
// console.log(items); Store.save(items);
// console.log(items); }
},
deep:true
},
methods:{
toggleFinish:function(item){
// console.log(item);
item.isFinished=!item.isFinished;
console.log(this.items);
},
addNew:function(){
// this.newItem;
// console.log(this.newItem);
this.items.push({
label:this.newItem,
isFinished:false
})
this.newItem='';
}
}
}
</script> <style>
.finished{
text-decoration:underline;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

b、componentA.vue子组件

 <<template>
<div class="hello">
<h1>{{msg}}</h1>
<button v-on:click="onClickMe">click me!</button>
<h1>{{msgfromfather}}</h1>
</div>
</template>
<script>
export default{
data:function(){
return{
msg:'hello from component a'
}
},
props:['msgfromfather'],
methods:{
onClickMe:function(){
console.log(this.msgfromfather);
}
}
} </script> <style>
h1{
color: #42b983;
}
</style>

c、效果

2、子组件向父组件传值

a、app.vue父组件

<template>
<div id="app">
<h1>{{title}}</h1>
<h1 v-text="title"></h1>
<h1 v-html="title"></h1>
<input type="text" v-model="newItem" v-on:keyup.enter="addNew">
<ul>
<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">
{{item.label}}
</li>
</ul>
<p>child tells me:{{childwords}}</p>
<componentA msgfromfather='hongMaJu' v-on:child-tell-me-something='listenToMyBoy'></componentA>

<!-- <component-a></component-a> -->
</div>
</template> <script>
import Store from './Store'
import componentA from './components/componentA'
//console.log(Store)
export default { data () {
return {
title: '<span>?</span>this is a todolist',
items:Store.fetch(),
// items:[
// {
// label:'coding',
// isFinished:false // },
// {
// label:'walking',
// isFinished:true // }
// ],
newItem:'',
childwords:''
}
},
components:{componentA},
watch:{
items:{
handler:function(items){
// console.log(val,oldVal)
// console.log(items); Store.save(items);
// console.log(items); }
},
deep:true
},
methods:{
toggleFinish:function(item){
// console.log(item);
item.isFinished=!item.isFinished;
console.log(this.items);
},
addNew:function(){
// this.newItem;
// console.log(this.newItem);
this.items.push({
label:this.newItem,
isFinished:false
})
this.newItem='';
},
listenToMyBoy:function(msg){
this.childwords=msg;
}

}
}
</script> <style>
.finished{
text-decoration:underline;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

b、componentA.vue子组件

 <<template>
<div class="hello">
<h1>{{msg}}</h1>
<button v-on:click="onClickMe">click me!</button>
<h1>{{msgfromfather}}</h1>
</div>
</template>
<script>
export default{
data:function(){
return{
msg:'hello from component a'
}
},
props:['msgfromfather'],
methods:{
onClickMe:function(){
// console.log(this.msgfromfather);
this.$emit('child-tell-me-something',this.msg)
}
}
} </script> <style>
h1{
color: #42b983;
}
</style>

c、效果

vue子父组件的通信的更多相关文章

  1. vue子父组件通信

    之前在用vue写子父组件通信的时候,老是遇到问题!!! 子组件传值给父组件: 子组件:通过emit方法给父组件传值,这里的upparent是父组件要定义的方法 模板: <div v-on:cli ...

  2. Vue子父组件方法互调

    讲干货,不啰嗦,大家在做vue开发过程中经常遇到父组件需要调用子组件方法或者子组件需要调用父组件的方法的情况,现做一下总结,希望对大家有所帮助. 父组件调用子组件方法: 1.设置子组件的ref,父组件 ...

  3. Day10-微信小程序实战-交友小程序-实现删除好友信息与子父组件间通信

    回顾:上一次已经把消息的布局以及样式做好了 效果图: 在removeList.js文件中,messageId就是发起这个消息的用户了 先查看一下自定义组件的生命周期 https://developer ...

  4. vue --子父组件传值

    1.父组件可以使用 props 把数据传给子组件. 2.子组件可以使用 $emit 触发父组件的自定义事件. vm.$emit( event, arg ) //触发当前实例上的事件 vm.$on( e ...

  5. vue子父组件传值

    https://blog.csdn.net/weixin_38888773/article/details/81902789 https://blog.csdn.net/jsxiaoshu/artic ...

  6. Vue子->父组件传值

    父组件引入: Import Test from'' 父页面使用: <Test ref="test" @m1="m2"><Test/> 子 ...

  7. Vue.js组件的通信之父组件向子父组件的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. vue 子父组件之间的通信,及在调用组件的地方

    这里是用了 element ui 你们也可以看一下管方的文档 http://element.eleme.io/#/zh-CN/component/installation 组件html <div ...

  9. vue之子父组件通信

    一. 子-父组件间通信: let children = {    # 定义子组件 template: `<div> <button @click="send"&g ...

随机推荐

  1. List实体去重

    public static ArrayList<Room> removeDuplicate(List<Room> room) { Set<Room> set = n ...

  2. poj 1579 Function Run Fun 【记忆化递归】

    <题目链接> 题目大意: 给出一些递归式,直接套用这些递归式计算. 解题分析: 递归式已经由题目明确说明了,但是无脑递归铁定超时,所以此时,我们需要加上记忆化,对于那些已经算过的,就没有必 ...

  3. CRC类(处理ITU表)

    class Crc { // CRC-ITU查找表 private static UInt16[] crctab16 = new UInt16[] { 0x0000, 0x1189, 0x2312, ...

  4. 服务端、实时、大数据、AI计算

    服务端.实时.大数据.AI计算,各种各样的计算,计算机本质是什么,计算机的本质是 利用compute的计算速度为人提供更优的计算结果. 所以实时也好,准实时.离线.AI本质上是两个维度,实时准实时强调 ...

  5. LOJ.6284.数列分块入门8(分块)

    题目链接 \(Description\) 给出一个长为n的数列,以及n个操作,操作涉及区间询问等于一个数c的元素,并将这个区间的所有元素改为c. \(Solution\) 模拟一些数据可以发现,询问后 ...

  6. [清橙A1210]光棱坦克

    [清橙A1210]光棱坦克 题目大意: 平面上放置了\(n(n\le7000)\)个反射装置,光纤将从某个装置出发,在经过一处装置时发生反射,若经过的装置坐标依次为\((x_1,y_1),(x_2,y ...

  7. 使用 P6Spy 来格式化 SQL 语句,支持 Hibernate 和 iBATIS

    事情起因 在处理一个查询小功能的时候,自认为 SQL 语句和传参均正确,然而查询结果无匹配数据,在查看 Hibernate 自带 SQL 语句输出的时候带着问好感觉有点不爽,特别是想复制 SQL 语句 ...

  8. CNUTCon2017全球运维技术大会(持续更新中) - 斯达克学院 - 实战驱动的 IT 教育平台 - Powered By EduSoho

    CNUTCon2017全球运维技术大会(持续更新中) - 斯达克学院 - 实战驱动的 IT 教育平台 - Powered By EduSoho   https://new.stuq.org/cours ...

  9. How to modify analog output range of Arduino Due

    Voltage Translation for Analog to Digital Interface ADC How to modify analog output range of Arduino ...

  10. 绘图 Painter转接口封装的方式

        记录下思想 适用于业务逻辑相对单纯的一些画法,比如画背景(颜色,背景,边框等) 一个Draw方法中如果绘制比较复杂的话,就会导致代码混乱,而不灵活,每次需求更改就得重新画过,可重用性差. 以接 ...