vue 子组件向父组件传递信息

1.要引入vue,js

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <div id="app">
            <parent>

            </parent>

        </div>
        <template id="parent">
            <div>
                <p>父组件</p>
                <h1 v-text="name"></h1>
                <p>{{"这个是子组件的数据:"+gg}}</p>
                <chlid @zdy="getdata"></chlid>
            </div>

        </template>
        <template id="chlid">
            <div>
                <p>子组件</p>
                <h2 v-text="na"></h2>
                <button @click="send">子组件向父组件传递信息</button>
            </div>

        </template>
        <script src="../vue.js"></script>
        <script type="text/javascript">
            new Vue({
                el: "#app",
                data: {

                },
                components: {
                    "parent": {
                        data() {
                            return {
                                name: "这个是父组件",
                                str: "简介",
                                gg: ""
                            }
                        },
                        methods: {
                            getdata(data) {
                                this.gg = data;
                            }
                        },
                        template: '#parent',
                        components: {
                            "chlid": {
                                props: ["a", "b"],
                                data() {
                                    return {
                                        na: "这个是子组件",
                                        name: "123456"
                                    }

                                },
                                template: '#chlid',
                                methods: {
                                    send() {
                                        this.$emit("zdy", this.name)
                                    }

                                }
                            }
                        }
                    }

                }
            })
        </script>
    </body>

</html>

猜你喜欢

转载自blog.csdn.net/weixin_41615439/article/details/83047779