zoukankan      html  css  js  c++  java
  • 组件通信-子传父

    <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>
  • 相关阅读:
    如何为ubuntu server 14.04 安装图形界面
    linux远程拷贝命令-scp
    Git和Github简单教程
    DotNetNuke
    Orchard
    JIRA-6.3.6安装与破解
    BugFree的安装
    webapi部署到IIS 404错误
    Mysql分组求和&LIMIT
    Mysql正则
  • 原文地址:https://www.cnblogs.com/nanjo4373977/p/12903114.html
Copyright © 2011-2022 走看看