vue子父组件的通信
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子父组件的通信的更多相关文章
- vue子父组件通信
之前在用vue写子父组件通信的时候,老是遇到问题!!! 子组件传值给父组件: 子组件:通过emit方法给父组件传值,这里的upparent是父组件要定义的方法 模板: <div v-on:cli ...
- Vue子父组件方法互调
讲干货,不啰嗦,大家在做vue开发过程中经常遇到父组件需要调用子组件方法或者子组件需要调用父组件的方法的情况,现做一下总结,希望对大家有所帮助. 父组件调用子组件方法: 1.设置子组件的ref,父组件 ...
- Day10-微信小程序实战-交友小程序-实现删除好友信息与子父组件间通信
回顾:上一次已经把消息的布局以及样式做好了 效果图: 在removeList.js文件中,messageId就是发起这个消息的用户了 先查看一下自定义组件的生命周期 https://developer ...
- vue --子父组件传值
1.父组件可以使用 props 把数据传给子组件. 2.子组件可以使用 $emit 触发父组件的自定义事件. vm.$emit( event, arg ) //触发当前实例上的事件 vm.$on( e ...
- vue子父组件传值
https://blog.csdn.net/weixin_38888773/article/details/81902789 https://blog.csdn.net/jsxiaoshu/artic ...
- Vue子->父组件传值
父组件引入: Import Test from'' 父页面使用: <Test ref="test" @m1="m2"><Test/> 子 ...
- Vue.js组件的通信之父组件向子父组件的通信
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue 子父组件之间的通信,及在调用组件的地方
这里是用了 element ui 你们也可以看一下管方的文档 http://element.eleme.io/#/zh-CN/component/installation 组件html <div ...
- vue之子父组件通信
一. 子-父组件间通信: let children = { # 定义子组件 template: `<div> <button @click="send"&g ...
随机推荐
- Unity 之 中文乱码
更改 C#脚本的编码格式: 文件 -> 高级保存选项 -> Unicode
- presistence
每一个神都是从弱到强的,像继科,在2011年之前,人很浮躁,球不稳,只是偶尔打出高质量而已:在输了无数场球之后,球厚了,人也定了(刘国梁评价),才抓住的机会成就了最快大满贯,并且创造了之后的辉煌,继科 ...
- Java中A instanceof B是什么意思?
instanceof用来判断内存中实际对象A是不是B类型 出现这种情况经常是需要强制转换的时候class Dog extends Animal譬如dog定义了自己的方法wangwang Animal ...
- 获取运行端口监听的用户身份auth-owner
获取运行端口监听的用户身份auth-owner Windows系统提供工作在TCP 113端口的授权服务(Authentication Service),用来判断TCP连接的用户.Nmap的aut ...
- 洛谷.1333.瑞瑞的木棍(欧拉路径 Hash)
题目链接 #include <cstdio> #include <cstring> const int N=2e6+5,M=5e5+5,mod=2e6; const int s ...
- HDU.2516.取石子游戏(博弈论 Fibonacci Nim)
题目链接 \(Description\) 1堆石子有n个.两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍,取完者胜.问谁能赢. \(Solution ...
- 解决 ajax 不能下载文件的问题
- 【转载】IntelliJ IDEA 内存优化最佳实践
本文转自 http://blog.oneapm.com/apm-tech/426.html [编者按]本文作者在和同事的一次讨论中发现,对 IntelliJ IDEA 内存采用不同的设置方案,会对 I ...
- Comparison method violates its general contract 解决
java.lang.IllegalArgumentException: Comparison method violates its general contract! 原因 JDK7中的Collec ...
- .Net Core Base64加密解密
一.Base64说明 1..Net Core中的Base64位加密解密和.Net Framework使用方式相同 2. Convert 类中提供了Base64位转码方法 Base64是网络上最常见的用 ...