这是根据官方提供的脚手架vue-cli搭建,通过简单的案例来介绍vue数据的传递的方式,根据自己平时用到的,来做简单的总结;

1.父组件传递数据给子组件

父组件传递数据给子组件,需要把子组件引入,并挂载到当前父组件的vue实例上,这样父组件就能访问到该组件。子组件上自定一个方法来作为传的桥梁,在子组件上使用props来接收数据;

父组件:

 1 <template>
2 <div class="hello">
3 <child-comp v-bind:send-info='sendInfo'></child-comp>
4 </div>
5 </template>
6
7 <script>
8 //引入子组件
9 import childComp from './son';
10 export default {
11 name: 'father',
12 data () {
13 return {}
14 },
15 computed:{
16 //需要传递的信息
17 sendInfo(){
18 let sendSonInfo;
19 sendSonInfo ={
20 age:'18',
21 name:'zhangsan'
22 };
23 return sendSonInfo;
24 }
25 },
26 //挂载到vue的实例上
27 components:{
28 childComp
29 }
30 }
31 </script>

子组件:

<style type="text/css"></style>
<template>
<div class="son">
<div class="name">
{{sendInfo.name}}
</div>
<div class="age">
{{sendInfo.age}}
</div>
</div>
</template>
<script type="text/javascript">
export default {
name:'son',
data(){
return {}
},
props:{
//子组件接收父组件传递来的数据
sendInfo:{
type:Object, //传递的数据类型
required:true, //是否必须
default:{} //默认传递类型
}
}
}
</script>

在父组件中引入子组件,使用v-bind(缩写:)动态绑定组件prop到表达式;

在子组件中使用props来接收传递过来的数据;

2.子组件传递给父组件数据

既然父组件能传递数据给子组件数据,那么子组件也要能出传递数据给父组件,同样也要在父组件引入,挂载,同时要定义一个方法来接收子组件传递来的数据,而子组件通过 $emit 来实现数据传递;第一个参数是父组件定义的方法名,第二个参数是要传递的数据,可以是字符串,Boolean,对象,数组等;

子组件:

<style type="text/css"></style>
<template>
<div class="son"></div>
</template>
<script type="text/javascript">
export default {
name:'son',
data(){
return {}
},
mounted(){
this.$emit('get-info','我是子组件传递来的数据');
}
}
</script>

父组件:

<template>
<div class="hello">
<child-comp v-on:get-info='getInfo'></child-comp>
<div>{{data}}</div>
</div>
</template> <script>
//引入组件
import childComp from './son';
export default {
name: 'father',
data () {
return {
data:'' // 定义变量来接收子组件传递来的数据
}
},
methods:{
//接收子组件传递来的数据
getInfo(val){
this.data = val;
}
},
//挂载到vue的实例上
components:{
childComp
}
}
</script>

父组件接收子组件数据,用v-on(缩写@)绑定事件监听器。

3.兄弟组件的数据传递

在父子组件数据的传递的过程,还有兄弟组件的数据传递。兄弟组件的传递需要借助于一个空的vue实例来实现,如果传递的数据量不大,可以用此方法,如果大量数据的传输,要使用vuex来实现数据的传递。下面实例来展示非vuex方式的兄弟组件的数据传递;

  1. 在main.js里的实例上挂载一个空的vue实例,来作为兄弟组件数据传递的桥梁

    // The Vue build version to load with the `import` command
    // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
    import Vue from 'vue'
    import App from './App'
    import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */
    new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }, data:{
    eventBus:new Vue() //创建eventBus的vue实例,作为桥梁
    }
    })

    2.在父组件中引入兄弟组件

    <template>
    <div class="hello">
    <child-comp></child-comp>
    <brother-comp></brother-comp>
    </div>
    </template> <script>
    //引入组件
    import childComp from './son';
    import brotherComp from './brother';
    export default {
    name: 'father',
    data () {
    return {}
    },
    components:{
    childComp,
    brotherComp
    }
    }
    </script>

    3.在组件里添加方法和要传递的数据

    <style type="text/css"></style>
    <template>
    <div class="son">
    <input type="button" name="" value="确定" @click='sendInfo'>
    </div>
    </template>
    <script type="text/javascript">
    export default {
    name:'son',
    data(){
    return {}
    },
    methods:{
    sendInfo(){
    this.$root.eventBus.$emit('sendBrotherinfo','我是兄弟组件');
    }
    }
    }
    </script>

    4.在兄弟组件里接收传来的数据

    <template>
    <div class="brother">
    <div>{{data}}</div>
    </div>
    </template>
    <script type="text/javascript">
    export default{
    name:'brother',
    data(){
    return {
    data:'' //声明变量来接收
    }
    },
    created(){
    this.$root.eventBus.$on('sendBrotherinfo', val =>{
    console.log(val);
    this.data = val;
    });
    }
    }
    </script>

    以上组件只能实现单个页面,不同组件的数据传递,如果想要在不同页面的数据传递,这就要借助于vuex来实现,下一篇来介绍vuex和使用vuex进行数据传递;

vue 2.x之组件的数据传递(一)的更多相关文章

  1. Vue之单文件组件的数据传递,axios请求数据及路由router

    1.传递数据 例如,我们希望把父组件的数据传递给子组件. 可以通过props属性来进行传递. 传递数据三个步骤: 步骤1:在父组件中,调用子组件的组名处,使用属性值的方式往下传递数据 <Menu ...

  2. VUE ---(9)组件——props数据传递

    本篇资料来于官方文档: http://cn.vuejs.org/guide/components.html#Props 本文是在官方文档的基础上,更加细致的说明,代码更多更全. 简单来说,更适合新手阅 ...

  3. vue 组件间数据传递

    父组件向子组件传值 方法一: 子组件想要使用父组件的数据,需要通过子组件的 props 选项来获得父组件传过来的数据. 1.父组件parent.vue中代码: <template> < ...

  4. Vue系列(三):组件及数据传递、路由、单文件组件、vue-cli脚手架

    上一篇:Vue系列(二):发送Ajax.JSONP请求.Vue生命周期及实例属性和方法.自定义指令与过渡 一. 组件component 1. 什么是组件? 组件(Component)是 Vue.js ...

  5. 浅入深出Vue:子组件与数据传递

    上一篇了解了组件的概念及在使用中需要注意的地方.在面对单个组件逻辑复杂需要拆分时,难免会遇到父子组件之间数据传递的问题.那么我们来了解一下在父子组件之间进行数据传递时需要遵循哪些约定,以及要注意哪些问 ...

  6. 13. VUE 组件之间数据传递

    组件数据传递: 父组件向内传递属性---动态属性 子组件向外发布事件 solt 插槽传递模板---具名solt 1. 父组件向子组件传递数据 子组件在父组件的并作为标签引入,通过设置标签的属性传递数据 ...

  7. react组件的数据传递

    在react中,为了解决html标签构建应用的不足,将公共的功能单独抽离成一个文件作为一个组件,在使用的地方按需引入,既然是组件彼此调用,就会涉及到父子组件的通信,下面主要来总结简单的组件通信. 1, ...

  8. vue 组件中数据传递

    //有种形式的传递:从父到子,从子到父,平行级别的传递//首先第一种:从父到子,用props属性绑定 //父级数据: new vue({ "el":"#app" ...

  9. React和Vue组件间数据传递demo

    一.React (一)父组件向子组件传数据 简单的向下传递参数 /* Parent */ class App extends Component { render() { return ( <d ...

随机推荐

  1. java 发架包

    // 完整发布流程 clean compile deploy install -U -DskipTests // 把架包安装到本地,跳过测试 install -Dmaven.test.skip=tru ...

  2. ABP 框架代码批量生成器

    需要最新源码,或技术提问,请加QQ群:538327407 我的各种github 开源项目和代码:https://github.com/linbin524 简介 用abp 框架快两年了,用它完成了多个项 ...

  3. [ASP.NET]关于DOT NET的IIS配置LocalHost访问和127.0.0.1访问的区别

    项目上遇到一个问题跟大家分享下,配置的localhost地址本地无法访问接口,外网却可以访问,查其原因百度资料比较全面的解释 localhost与127.0.0.1的概念和工作原理之不同 要比较两个东 ...

  4. c# .net中的简单Job

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Spring Boot - Spring Data

    使用JPA 虽然JPA是一个标准,但spring中一般就是使用hibernate实现的 使用JPA(Java Persistence API,Java持久化API,是个规范,其实是借助Hibernat ...

  6. 201621123018《Java程序设计》第2周学习报告

    Week02-Java基本语法与类库 1.本周学习总结 Java数据类型分为基本数据类型和引用数据类型.布尔型是Java特有的数据类型.本周重点学习了字符串类型String,String类型中==和e ...

  7. uiautomator2

    uiautomator2    该项目正在火热的开发中 uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库.其底层基于Google uiautomator,Go ...

  8. django Form的回顾--手动档和自动挡

      from django.shortcuts import renderfrom django.forms import Formfrom django.forms import fieldsfro ...

  9. python 使用dir() help() 查看一个对象所有拥有的方法和属性

    可以使用python 的内置方法 dir() 或者help() 查看 某个对象所拥有的方法和属性, 二者间的区别是: dir() : 只是得到方法或者属性的名称 help():不但可以得到对象的方法和 ...

  10. Django 框架中定时触发脚本

    方法一: Django搭建的服务器一般都用作WEB网站进行访问,通常的形式是用户访问网站或点击按钮发送请求,Django检测到请求后进行相应的试图函数处理后返回页面给用户. 但是,我们有时会需要有一些 ...