这是根据官方提供的脚手架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. Python学习-23.Python中的函数——isinstance

    在Python中可以使用isinstance函数来判断某个值或变量是否为某个类型. 例子: print(isinstance(1,int)) print(isinstance(1,float)) pr ...

  2. 将Heap RID转换成RID格式

    使用DBCC PAGE命令查看索引叶子层中的索引键(最后一个参数为3才会显示索引键对应的HEAP RID)时,可以使用下面的SQL将HEAP RID转换成 FileId:PageId:SlotNo的格 ...

  3. [Openwrt 项目开发笔记]:USB挂载& U盘启动(三)

    [Openwrt项目开发笔记]系列文章传送门:http://www.cnblogs.com/double-win/p/3888399.html 正文: 在上一篇中,我结合Netgear Wndr370 ...

  4. [c# 20问] 4.Console应用获取执行路径

    一行代码可以搞定了~ static void GetAppPath() { string path = System.Reflection.Assembly.GetExecutingAssembly( ...

  5. 数据库选项--ALTER DATABASE WITH 选项

    指定当数据库从一种状态转换到另一种状态时,何时回滚未完成的事务. 如果终止子句被忽略,则当数据库中存在任何锁时,ALTER DATABASE 语句将无限期等待. 只能指定一条终止子句,而且该子句应跟在 ...

  6. 使用vs code开发纸壳CMS并启用Razor智能提示

    关于纸壳CMS 纸壳CMS是一个开源免费的,可视化设计,在线编辑的内容管理系统.基于ASP .Net Core开发,插件式设计: 下载代码 GitHub:https://github.com/Seri ...

  7. OI字符串 简单学习笔记

    持续更新qwq KMP 其实是MP啦qwq 就是先自己匹配自己得到状态图,然后再在上面进行模式串的匹配. nxt数组返回的是以该节点结尾的,最长的,在前面出现过的,不相交的,字符串的最靠右的,末位位置 ...

  8. Java基础学习篇---------多线程

    一.编写两种多线程的方法 (1).Thread(它是继承Runnable的子类) class MyThread extends Thread{ private int ticket = 5; @Ove ...

  9. Cordova - 实现热更新 !

    Cordova版本:8.0.0 更新APP平台:Android 操作系统:Windows Cordova的热更新,作用是把www内的变动部分更新到APP中,实现主程序不动,完成更新!!这个热更新功能, ...

  10. Memoization-329. Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...