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. Environment error: “CodeBloks can't find compiler executable in your configured search path's for GNU GCC compiler”

    codeblock安装后,提示cant find compiler executable in your configured search paths for GNU GCC Compiler 可能 ...

  2. Python爬虫之PyQuery使用(六)

    Python爬虫之PyQuery使用 PyQuery简介 pyquery能够通过选择器精确定位 DOM 树中的目标并进行操作.pyquery相当于jQuery的python实现,可以用于解析HTML网 ...

  3. Java—集合框架详解

    一.描述Java集合框架 集合,在Java语言中,将一系类的对象看成一个整体. 首先查看jdk中的Collection类的源码后会发现Collection是一个接口类,其继承了java迭代接口Iter ...

  4. codeforces-1114F-线段树练习

    https://www.cnblogs.com/31415926535x/p/10391639.html 概述 这是一道用线段树维区间值的一道题,,题意很简单,,就是对于给定的一个序列,,初始时每个数 ...

  5. DB安装

    start from the execute file : DB2_ESE_10_Win_x86-64\setup.exe Navigator to "Install a Product&q ...

  6. C# DataGridView插入DB

    public static bool ContrastColumns(DataColumnCollection co1, DataGridViewColumnCollection co2) { boo ...

  7. [CF241E]Flights

    [CF241E]Flights 题目大意: 给一张\(n(n\le1000)\)个点\(m(m\le5000)\)条边的DAG,确定每条边的边权\(w_i(w_i\in\{1,2\})\),使得所有从 ...

  8. node模块包装为Promise书写法

    1. const Promise = require('bluebird') const fs = Promise.promisifyAll(Promise.promisify(require('fs ...

  9. 20172302 《Java软件结构与数据结构》第八周学习总结

    2018年学习总结博客总目录:第一周 第二周 第三周 第四周 第五周 第六周 第七周 第八周 教材学习内容总结 第十二章 优先队列与堆 1.堆(heap)是具有两个附加属性的一棵二叉树: (1)它是一 ...

  10. chrome插件开发.在content_script异步加载页面后, 如何进行JS通信与调用的问题

    使用场景 在开发Chrome插件时, 有一种需求: 要求在WEB页面显示一个浮动窗口(A), 在此窗口中允许用Ajax方式调用另一个服务器上的一个页面(B) B页面上有独立的功能用JS写functio ...