组件基础-组件的作用域

<!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>
        <child1-component></child1-component>
        <child2-component></child2-component>
    </div>

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

        // 每个组件都有一个独立的作用域,组件之间不能直接访问对方的数据
        // 父子组件之间是相互独立的,不能访问对方的数据(组件的状态)
        // 兄弟组件之间是相互独立的,不能访问对方的数据(组件的状态)


        const app = new Vue({
            el: '#app',
            data: {
                rootMesage: 'app根组件的数据'
            },
            components: {
                'child1-component': {
                    data () {
                        return {
                            child1Message: 'child1组件的数据'
                        }
                    },
                    template: '<div><h2>{{child1Message}}</h2></div>'
                },
                'child2-component': {
                    data () {
                        return {
                            child2Message: 'child2组件的数据'
                        }
                    },
                    template: '<div><h2>{{child2Message}}</h2></div>'
                }
            }
        });

    </script>
</body>

</html>
发布了151 篇原创文章 · 获赞 1 · 访问量 1861

猜你喜欢

转载自blog.csdn.net/qq_45802159/article/details/103818250