美文网首页
父组件可以向子组件传参,子组件不可改变父组件数据

父组件可以向子组件传参,子组件不可改变父组件数据

作者: daoqing99 | 来源:发表于2018-04-17 10:26 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>ref</title>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    </head>
    
    <body>
        <div id="app">
            <counter :count="0" @change="handleSum"></counter>
            <counter :count="0" @change="handleSum"></counter>
            <div>{{sum}}</div>
        </div>
        <script>
        var counter = {
            props: ['count'],
            data: function() {
                return {
                    number: this.count,
                }
            },
            template: "<div @click='handleNumPlus'>{{number}}</div>",
            methods: {
                handleNumPlus: function() {
                    this.number = this.number + 1;
                    this.$emit('change', 1)
                }
            }
        };
    
        var vm = new Vue({
            el: '#app',
            data: {
                sum: 0
            },
            methods: {
                handleSum: function(step) {
                    this.sum += step;
                }
            },
            components: {
                counter: counter
            }
        })
        </script>
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:父组件可以向子组件传参,子组件不可改变父组件数据

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