首页 > 其他 > 详细

组件通信-子传父

时间:2020-05-17 01:28:28      阅读:77      评论:0      收藏:0      [点我收藏+]
<body>
    <div id="app">
        <!-- 3.使用子组件 -->
        <App></App>
    </div>
    <script>
        //全局组件
        //子传父:
        //1.在父组件中绑定自定义事件
        //2.在子组件触发原生的事件,在事件函数通过this.$emit触发自定义的事件

        Vue.component(‘Child‘, {
            template: `
                <div>
                    <h3>我是一个子组件</h3>   
                    <h4>{{childData}}</h4> 
                    <input type="text" @input = ‘handleInput‘/>
                </div>
            `,
            props: [‘childData‘],
            methods: {
                handleInput(e) {
                    const val = e.target.value;
                    this.$emit(‘inputHandler‘, val);
                }
            },
        })

        //1.创建局部组件
        //注意:在组件中这个data必须是一个函数,返回一个对象
        const App = {
            data() {
                return {
                    msg: ‘我是父组件传进来的值‘,
                    newVal: ‘‘
                }
            },
            methods: {
                input(newVal) {
                    this.newVal = newVal
                }
            },
            template: `
                <div>
                    <div class="father">
                        数据:{{newVal}}
                    </div>
                    <child :childData = ‘msg‘ @inputHandler = "input"></child>
                </div>
                
            `,
            computed: {

            },
        }

        new Vue({
            el: ‘#app‘,
            data: {

            },
            components: {
                //2.挂载子组件
                App
            }
        })
    </script>
</body>

 

组件通信-子传父

原文:https://www.cnblogs.com/nanjo4373977/p/12903114.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!