美文网首页
vue 子组件通过(订阅/发布)传递数据给父组件

vue 子组件通过(订阅/发布)传递数据给父组件

作者: ChenDaXia | 来源:发表于2017-12-02 20:30 被阅读0次
<!DOCTYPE html>
<html>

<head>
    <!-- 声明文档使用的字符编码 -->
    <meta charset='utf-8'>
    <title>Title of the document</title>
    <script type="text/javascript" src="vue.js"></script>
</head>

<body>
    <div id="app">
        <p>总数: {{ total }}</p>
        <my-component @increase="handleGetTotal" @reduce="handleGetTotal"></my-component>
    </div>
</body>
<script>
    Vue.component('my-component', {
        template: '\
        <div>\
            <button @click="handleIncrease">+1</button>\
            <button @click="handleReduce">-1</button>\
        </div>',
        data: function() {
            return {
                counter: 0
            }
        },
        methods: {
            handleIncrease: function() {
                this.counter++;
                this.$emit('increase', this.counter);
            },
            handleReduce: function() {
                this.counter--;
                this.$emit('reduce', this.counter);
            }
        }
    })
    var app = new Vue({
        el: '#app',
        data: {
            total: 0
        },
        methods: {
            handleGetTotal: function(total) {
                this.total = total;
            }
        },
        computed: {

        }
    })
</script>

</html>

相关文章

  • 2019-04-10父子组件的交互

    父组件通过属性的方式向子组件传递参数(数据)。 子组件通过发布订阅模式向父组件,子组件发布一个事件,父组件恰好之前...

  • vue父子组件以及非父子组件的通信

    1.父组件传递数据给子组件 通过props传递数据 2.子组件传递数据给父组件 通过$emit传递父组件数据 子组...

  • Vue-自定义事件

    在父组件使用prop 传递数据给子组件,子组件则是通过事件传递数据给父组件的。 Vue实例都会实现事件接口: 1....

  • Vue 组件数据传递

    Vue 组件数据传递 父组件->子组件 父组件到子组件的数据通过 props 传递 在父组件注册子组件,传递数据到...

  • Vue组件间数据传递

    前言 总结vue组件间的数据传递 路由传参 父组件传递数据给子组件---props 子组件传递数据给父组件---$...

  • vue组件嵌套(模态框)

    在vue中父组件向自子组件传递props; 子组件向父组件传递属性是用$emit(发布订阅); 实例基本逻辑:点击...

  • 弹窗设置(父子传参原理)

    父组件: 1.父组件向子组件传递数据父组件绑定属性,给子组件传递数据子组件通过props接收父组件传递过来的数据子...

  • react父子组件通信

    父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据。

  • Vue父子组件之间的通信

    在Vue中,父子组件的关系可以总结为,父组件通过prop向下传递,给子组件发数据,子组件通过事件给父组件发送消息。...

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

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

网友评论

      本文标题:vue 子组件通过(订阅/发布)传递数据给父组件

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