Vue组件通信 – props

最后更新于:2020-09-05 09:58:23 | 56 次浏览

Vue - Porps 父传子数据通信

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <my-div :getparent="m"></my-div>
    </div>
    <template id="mydiv">
        <div>
             <h1>你好:{{msg}}</h1>
             <h4>用来存放父组件传过来的信息:{{getparent}}</h4>
        </div>
    </template>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        new Vue({
            el:"#app",
            data:{
                m:"¥1000元"
            },
            components:{
                'my-div':{
                    template:"#mydiv",
                    props:['getparent'],
                    data() {
                        return {
                            msg:"我是子组件信息",
                        }
                    },
                }
            }
        });
    </script>
</body>
</html>