写在前面:

1.父组件的data写法与子组件的data写法不同

//父组件
data:{
//对象形式
} //子组件
data:function(){
return {
//函数形式
}
}

2.引用子组件遵循

  • 引入组件
  • components里定义使用
  • 如果有通信,需要在子组件的props注册

以下实例全部使用以下模板

<div id="app">
//父组件
<p>{{total}}</p>
<mime @increment1="incrementTotal" ref="child" :num-a="total" num-s="total"></mime> <button type="button" @click="clickref">调用子组件</button>
</div> //子组件
<template id="myInput">
<button @click="add">{{counter}}</button>
</template>
<script>
new Vue({
el:'#app',
data :{
total: 0
},
methods:{
incrementTotal : function(){ },
clickref:function(){ }
},
components:{
'mime' :{
template:'#myInput',
data : function(){
return{
counter : 0
}
},
props:['numA','numS'],
methods:{
add : function(){ }
}
}
}
});
</script>

1.父子通信 之 静态数据

如果只是传单一的字符串

<mime num-s="total"></mime>

....

props:['numS'] // numS  为字符串 total

这样子组件的numS一直为total。但这种太不灵活

2.父子通信 之 动态数据

父组件的数据将会动态传递给子组件

<input v-model="total">
<mime :num-a="total"></mime> .... //props:['numA'] props:{
numA:[ String , Number ] //允许字符串 数字
}

这时当input输入什么,子组件的numA将会得到什么

3.父子通信 之 子调用父

{{total}}
<mime @increment="incrementTotal"></mime> <template id="myInput">
<button @click="add">{{counter}}</button>
</template> ...
<script>
....
data:{
tatal: 0
},
methods:{
incrementTotal:function(){
this.total +=1;
}
},
components:{
data : function(){
return:{
counter : 0
}
},
methods : {
add : function(){
this.counter +=1;
this.$emit('increment'); //子组件通过 $emit触发父组件的方法 increment 还可以传参 this.$emit('increment' ,this.counter);
}
}
}
</script>

子组件执行add --> 触发$emit --> 触发父组件increment --> 执行 incrementTotal 方法

4.父子通信 之 父调用子

<mime ref="child"></mime>
<button type="button" @click="clickref">调用子组件</button> <template id="myInput">
<button @click="add">{{counter}}</button>
</template> ...
<script>
.... methods:{
clickref:function(){
var child = this.$refs.child; //获取子组件实例
child.counter = 45; //改变子组件数据
child.add(11); //调用子组件方法 add
}
},
components:{
data : function(){
return:{
counter : 0
}
},
methods : {
add : function(num){
this.counter +=1;
console.log('接受父组件的值:',num) //num为11
}
}
}
</script>

通过在子组件上引用ref,从而获得子组件实例,进行相应操作。

5.子组件与子组件通信

官网:在简单的场景下,使用一个空的 Vue 实例作为中央事件总线

var bus = new Vue()
// 触发组件 A 中的事件
bus.$emit('id-selected', 1)
// 在组件 B 创建的钩子中监听事件
bus.$on('id-selected', function (id) {
// ...
})

但是这样实在太麻烦了,建议使用vuex

 

vue2.0--组件通信(非vuex法)的更多相关文章

  1. vue2.0组件通信各种情况总结与实例分析

    Props在vue组件中各种角色总结 在Vue中组件是实现模块化开发的主要内容,而组件的通信更是vue数据驱动的灵魂,现就四种主要情况总结如下: 使用props传递数据---组件内部 //html & ...

  2. vue2.0 组件通信

    组件通信: 子组件要想拿到父组件数据 props 子组件不允许直接给父级的数据, 赋值操作如果想更改,父组件每次穿一个对象给子组件, 对象之间引用. 例子: <script> window ...

  3. vue2.0组件通信小总结

    1.父组件->子组件 父组件 <parent> <child :child-msg="msg"></child>//这里必须要用 - 代替 ...

  4. 通信vue2.0组件

    vue2.0组件通信各种情况总结与实例分析   Props在vue组件中各种角色总结 在Vue中组件是实现模块化开发的主要内容,而组件的通信更是vue数据驱动的灵魂,现就四种主要情况总结如下: 使用p ...

  5. vue2.0组件库

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  6. vue2.X 组件通信($emit $on props)

    1.index.html  子组件直接修改父组件的数据 组件通讯: vm.$emit(); vm.$on(); 父组件和子组件: 子组件想要拿到父组件数据: 通过 props 之前,子组件可以更改父组 ...

  7. vue2.0组件传值

    props down   emit up 嘿嘿    如果是第一次接触vue2.0组件传值的肯定很疑惑,这是什么意思(大神总结的,我也就是拿来用用) “down”—>指的是下的意思,即父组件向子 ...

  8. Vue2.0组件之间通信

    Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...

  9. Vue2.0组件之间通信(转载)

    Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...

随机推荐

  1. squid总结

    squid可以完成的工作: 代理服务器 反向代理服务器 防火墙 缓存功能 透明代理 squid和varnish的对比,以及squid的优缺点说明: 缓存到硬盘,容易遇到I/O瓶颈 V3.2以下不支持多 ...

  2. Axis2 WebService(配置、发布、调用)

    准备工作 1.下载:axis2-1.5.4-bin.zip,axis2-1.5.4-war.zip 下载地址:http://axis.apache.org/axis2/java/core/ 2.环境变 ...

  3. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  4. map/reduce之间的shuffle,partition,combiner过程的详解

    Shuffle的本意是洗牌.混乱的意思,类似于java中的Collections.shuffle(List)方法,它会随机地打乱参数list里的元素顺序.MapReduce中的Shuffle过程.所谓 ...

  5. jenkins Process leaked file descriptors

    https://stackoverflow.com/questions/17024441/process-leaked-file-descriptors-error-on-jenkins 1. BUI ...

  6. 省队集训 Day3 吴清华

    [题目大意] 给网格图,共有$n * n$个关键节点,横向.纵向距离均为$d$,那么网格总长度和宽度均为$(n+1) * d + 1$,最外围一圈除了四角是终止节点.要求每个关键节点都要通过线连向终止 ...

  7. 【BZOJ】1724 [Usaco2006 Nov]Fence Repair 切割木板

    [算法]贪心+堆 #include<cstdio> #include<algorithm> using namespace std; ; int n,heap[maxn],sz ...

  8. Centos 6.5下安装vsftpd服务器

    1.查看是否安装vsftp  [root@localhost ~]#rpm -qa|grep vsftpd 如果出现 vsftpd-2.2.2-13.el6_6.1.x86_64  则说明已经安装了v ...

  9. Angular2.0 基础: 环境搭建

    最近在学习Angular2的使用,其实看过Angular2 文档的都知道,相比于之前的Angular1,Angular2 的改动还是挺大的. 而对于‘angular2 的本地开发环境的搭建的中,我们首 ...

  10. python进行机器学习(五)之模型打分

    一.画出模型的残差值分布情况 #!/usr/bin/python import pandas as pd import numpy as np import csv as csv import mat ...