父组件为子组件传递数据(静态数据)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <div id="app">
        <h1>{{rootMesage}}</h1>

        <!-- 给子组件传递了一个字符串:'Hello World' -->
        <child1-component  message="Hello World" second-message="你好,世界" thirdMessage="第三条消息"></child1-component>
    </div>

    <script src="./vue.js"></script>
    <script>

        // HTML 标签的属性名是不区分大小写,即使你写的是大写的{例如,thirdMessage ),也会被作为小写的对待(例如 thirdmessage)。
        // 如果出现了同名的属性,则只接受第一个属性的值,其他会被忽略。
        // 如果传入的属性名有多个单词组成,则可以将多个单词以连接符(-)分隔,
        // 子组件在接收参数时,需要将连接符去掉,然后将连接符后面的单首字母大写。

        const app = new Vue({
            el: '#app',
            data: {
                rootMesage: 'app根组件的数据'
            },
            components: {
                'child1-component': {
                    props: ['message', 'secondMessage', 'thirdmessage'],
                    data() {
                        return {
                            child1Message: 'child1组件的数据'
                        }
                    },
                    template: `<div>
                    <h2>{{child1Message}}</h2>
                    <h2>{{message}}</h2>
                    <h2>{{secondMessage}}</h2>
                    <h2>{{thirdmessage}}</h2>
                    </div>`
                }
            }
        });

        // props properties 属性
        // ref   reference  引用

    </script>
</body>

</html>

posted @ 2022-10-21 10:39  张尊娟  阅读(22)  评论(0编辑  收藏  举报