431 vue局部组件用法

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

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

<body>
    <div id="app">
        <hello-world></hello-world>
        <hello-tom></hello-tom>
        <hello-jerry></hello-jerry>
        <test-com></test-com>
    </div>
    <script type="text/javascript" src="js/vue.js"></script>
    <script type="text/javascript">
        /**/
        /*
            局部组件注册
            局部组件只能在注册他的父组件中使用
        */

        Vue.component('test-com', {
            // 报错,因为 hello-world 是局部组件,只能用在父组件app中,不能用在这里
            template: '<div>Test -- <hello-world></hello-world> </div>'
        });

        var HelloWorld = {
            data: function() {
                return {
                    msg: 'HelloWorld'
                }
            },
            template: '<div>{{msg}}</div>'
        };

        var HelloTom = {
            data: function() {
                return {
                    msg: 'HelloTom'
                }
            },
            template: '<div>{{msg}}</div>'
        };

        var HelloJerry = {
            data: function() {
                return {
                    msg: 'HelloJerry'
                }
            },
            template: '<div>{{msg}}</div>'
        };

        var vm = new Vue({
            el: '#app',
            data: {

            },
            components: {
                'hello-world': HelloWorld,
                'hello-tom': HelloTom,
                'hello-jerry': HelloJerry
            }
        });
    </script>
</body>

</html>

posted on 2020-03-31 16:36  冲啊!  阅读(426)  评论(0编辑  收藏  举报

导航