Vue的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据,必须使用特定的方法才能实现组件之间的数据传递。

下列为在vue-cli创建项目中的操作

一·父组件向子组件传递数据

在Vue中,用props向子组件传递数据。

子组件部分:
 <template>
<div class='header'>{{logo}}</div>
</template>
<script>
export default{
name:"headerDiv",
data(){
return {
............
}
},
props:["logo"]
}
</script> 

如果需要从父组件获取logo值,就需要使用props:['logo']

在props中添加了元素之后,就不需要在data中再添加变量了

父组件部分:

 <template>
<div id='app'>
<HeaderDiv :logo="logoMsg"></HeaderDiv>
</div>
</template>
<script>
import HeaderDiv from './compontents/header' export default{
name:'app',
data(){
return {
logoMsg:'VUE'
}
},
components:{
HeaderDiv
}
}
</script>

二·子组件向父组件传递数据

子组件主要通过事件传递数据给父组件

子组件部分:

 <template>
<div class='header'>
<input v-model="name" @change="getCh">
</div>
</template>
<script>
export default {
name:'header',
data(){
return {
name:''
}
},
methods:{
getCh:function(){
this.$emit('setCh',this.name)
}
}
}
</script>

当name变化时,将name传给父组件,

在getCh中用$emit来遍历setCh事件,并返回this.name

setCh是一个自定义事件,this.name通过该事件传递给父组件

父组件部分:

<template>
<div id='app'>
<HeaderDiv @trans='getCh'></HeaderDiv>
<div>{{user}}</div>
</div>
</template>
<script>
import HeaderDiv from './components/header'
export default {
name:'app',
data(){
return {
name:''
}
},
methods:{
getCh(msg){
this.name=msg
}
},
components:{
HeaderDiv
}
}
</script>

三·子组件互相传值

1.在main.js里全局定义eventBus

2.firstchild.vue

<template>
<div>
<button @click="btn">我是子组件一</button>
</div>
</template>
<style type="text/css"> </style>
<script type="text/javascript">
export default{
name:'Firstchild',
methods:{
btn(){
console.log('start');
eventBus.$emit('name','hello')
}
}
}
</script>

3.secondchild.vue

<template>
<div>
<button @click="btn2">我是子组件二</button>
</div>
</template>
<style type="text/css"> </style>
<script type="text/javascript">
export default{
name:'Secondchild',
methods:{
btn2(){
console.log('end');
eventBus.$on('name',function(val){
console.log('我是firstchild组件传过来的'+val)
})
}
}
}
</script>  

运行结果

Vue之组件之间的数据传递的更多相关文章

  1. 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function https://www.cnblogs.com/jaso ...

  2. 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用  props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要 ...

  3. Vue 爬坑之路(二)—— 组件之间的数据传递

    Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. 首先用 vue-cli 创建一个项目,其中 App.vue 是父组件,com ...

  4. Vue基础知识之组件及组件之间的数据传递(五)

    vue中的组件是自定的标签,可以扩展的原生html元素,封装可复用的代码 note: 1.在标签命中不要使用大写,标签名字必须用短横线隔开 2.模板中只能有一个根元素,不能使用并列标签. 定义组件 全 ...

  5. (转)Vue 爬坑之路(二)—— 组件之间的数据传递

    Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. 首先用 vue-cli 创建一个项目,其中 App.vue 是父组件,com ...

  6. React中父组件与子组件之间的数据传递和标准化的思考

    React中父组件与子组件之间的数据传递的的实现大家都可以轻易做到,但对比很多人的实现方法,总是会有或多或少的差异.在一个团队中,这种实现的差异体现了每个人各自的理解的不同,但是反过来思考,一个团队用 ...

  7. Angular06 组件、模块、父子组件之间的数据传递

    1 创建组件 进入到angular项目的根目录,执行如下命令 ng g component test-component 注意:执行完上述命令后在angular项目的src/app文件夹下就会多出一个 ...

  8. vue 组件之间的数据传递

    父组件传数据给子组件 创建数据 获取json数据 子组件传数据给父组件 1. 子组件使用$emit监听数据 getAreaValues(){ this.$emit('getAreaValues', { ...

  9. vue2.0 组件之间的数据传递

    组件间的数据传递// 父组件<template><div class="order"><dialog-addpro v-on:closedialog= ...

随机推荐

  1. emacs 下 common lisp 配置

    安装 sbcl .emacs 加入 ;for lisp mode (add-to-list 'load-path "D:/kuaipan/.emacs.d/elpa/slime-201311 ...

  2. XML Publisher Template Type - Microsoft Excel Patch

    XML Publisher Template Type - Microsoft Excel Patch Oracle XML Publisher > Templates > Create ...

  3. weblogic内存调整说明

    一:WebLogic配置问题:  由于WebLogic的配置问题,我们的测试出现了失败情况.原因是为WebLogic分配的内存太少了.通过修改commom\bin\commEnv.cmd文件来增加内存 ...

  4. 【转】fedora和ubuntu开启ssh

    fedora和ubuntu开启ssh 1小时前 ubuntu开启SSH服务 SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装opens ...

  5. annotation使用示例

    annotation使用示例 学习了:https://www.imooc.com/learn/456 Annotation编写规则:@Target,@Retention,设置一些String.int属 ...

  6. BeagleBone Black Industrial 工业版介绍

    前言 在电子发烧友论坛看到有Beaglebone Black Industrial版的试用,这里介绍一下这块开发板. BBB是开源硬件,原理图.BOM等都开放下载,所以也有诸多兼容板. BBB兼容产品 ...

  7. TListView使用方法1(转)

    ListView1.Items 为标准 Tlistitems类 ListView1.Items (1)赋值 with ListView1.Items.Add do begin Caption:=cap ...

  8. zedboard中OLED源码

    #include <stdio.h> #include "platform.h" #include "xil_types.h" #include & ...

  9. Java学习之基本概念

    1.java体系结构: j2se:是Java的标准版,用于标准的应用开. j2ee:是Java的企业版,用于企业级的应用服务,web服务开发. j2me:是Java的微型版,用户手机或嵌入式开发. p ...

  10. Spark Streaming性能优化系列-怎样获得和持续使用足够的集群计算资源?

    一:数据峰值的巨大影响 1. 数据确实不稳定,比如晚上的时候訪问流量特别大 2. 在处理的时候比如GC的时候耽误时间会产生delay延迟 二:Backpressure:数据的反压机制 基本思想:依据上 ...