美文网首页
【Vue学习笔记】—— 组件之间传递数据 { }

【Vue学习笔记】—— 组件之间传递数据 { }

作者: Curry_Ming | 来源:发表于2020-12-20 20:04 被阅读0次

学习笔记

  • 作者:oMing

Vue 组件
1.通过绑定传递数据(父组件 ——》 子组件)
<div id="app">
    <father></father>
</div>

<template id="father">
    <div>
        <h1>father:{{ fatherMsg.name }} -- {{ fatherMsg.age }}</h1>
        <son :faobj='fatherMsg'></son>
    </div>
</template>
<template id="son">
    <div>
        <input type="button" @click='getFather' value="获取一个父亲">
        <h1>小头儿子:my father -- {{ faobj.name }} -- {{ faobj.age }}</h1>
    </div>
</template>
Vue.component('father', {
    template: '#father',
    data: function () {
        return {
            fatherMsg: {
                name: '小头爸爸',
                age: '21'
            }
        }
    },
    components: {
        son: {
            template: '#son',
            data: function () {
                return {
                    fobj: {
                        name: '',
                        age: ''
                    }
                }
            },
            props: ['faobj'],
            methods: {
                getFather() {
                    this.fobj = this.faobj
                }
            }
        }
    }
})
new Vue({
    el: "#app",
})
2.通过事件传递数据 (子组件 ——》 父组件)
<div class="app">
    <father></father>
</div>
<template id="father">
    <div>
        <h1>father: mySon -- {{ sonObj.name }} -- {{ sonObj.age }}</h1>
        <son @stof='getSon'></son>
    </div>
</template>
<template id="son">
    <div>
        <h1>son: {{ sobj.name }} -- {{ sobj.age }}</h1>
        <input type="button" value="投入到父亲的怀抱" @click='sonToFather'>
    </div>
</template>
Vue.component('father', {
    template: '#father',
    data: function () {
        return {
            sonObj: {
                name: '',
                age: ''
            }
        }
    },
    methods: {
        getSon(son) {
            this.sonObj = son
        }
    },
    components: {
        son: {
            template: '#son',
            data: function () {
                return {
                    sobj: {
                        name: '大耳朵图图',
                        age: '1'
                    }
                }
            },
            methods: {
                sonToFather() {
                    this.$emit('stof', this.sobj)
                }
            }
        }
    }
})

new Vue({
    el: ".app",
})

相关文章

  • 【Vue学习笔记】—— 组件之间传递数据 { }

    学习笔记 作者:oMing Vue 组件1.通过绑定传递数据(父组件 ——》 子组件) 2.通过事件传递数据 ...

  • vue之props数据传递(单向,双向sync)

    学习vue必不可少的一个环节就是数据之间的传递,下面我们详细介绍下父组件如何向子组件传递数据的。 1组件实例的作用...

  • Vue2.x开发饿了么项目(header部分)

    header组件开发 数据之间的传递 App.vue 引入组件,App.vue 中注册组件,使用组件时 要记得传...

  • vue 事件总线EventBus的概念、使用以及注意点

    vue组件中的数据传递最最常见的就是父子组件之间的传递。父传子通过props向下传递数据给子组件;子传父通过$em...

  • vue 中几种传值方法

    前言 vue项目中,组件跟组件之间数据的传递是很普遍的行为,在这里总结几种常见的vue组件跟组件之间传值方式,其中...

  • 关于$on的用法

    $on 兄弟组件之间相互传递数据 示例 父组件 $onFa.vue 创建一个vue的空白实例 emptyVue.j...

  • Vue组件之间传递数据

    本篇主要内容: 1、父组件向子组件传值 2、父组件向子组件传递方法 3、将整个父组件传给子组件 4、父组件主动获取...

  • Vue组件通信

    最近在学习Vue,组件化后不同组件之间如何通信,记录一下。 Vue中组件通信时,数据流只能由父级传递给子级,通过p...

  • vue父子组件传递数据方法

    标签(空格分隔): vue 父组件向子组件传递数据 Vue中父组件向子组件传递数据用props 举个例子 父组件中...

  • VUE的props理解

    vue的组件之间是独立的,为了在组件之间传递数据,我们需要用到不同的手段。父组件->子组件:props子组件->父...

网友评论

      本文标题:【Vue学习笔记】—— 组件之间传递数据 { }

      本文链接:https://www.haomeiwen.com/subject/xtnknktx.html