WEB前端:vuejs全家桶(22):组件的分类:全局组件、局部组件

3. 组件的分类

分类:全局组件、局部组件

全局组件

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

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div class="itany">
        <my-world></my-world>
    </div>
</body>
<script>
    //   全局组件,可以在所有vue实例中使用
    Vue.component('my-world', {
        template: '<h1>你好,世界!</h1>'
    });

    new Vue({
        el: '.itany'
    })
</script>

</html>

局部组件,只能在当前vue实例中使用

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

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div class="itany">
        <my-world></my-world>
    </div>
</body>
<script>
    new Vue({
        el: '.itany',
        components: { //局部组件,只能在当前vue实例中使用
            'my-world': {
                template: '<h3>welcome to guangzhou</h3>'
            }
        }
    })
</script>

</html>

全局组件、局部组件,数据绑定

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

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div class="itany">
        <my-hello></my-hello>
        <my-world></my-world>
    </div>
</body>
<script>
    /**
     * 全局组件,可以在所有vue实例中使用
     */
    Vue.component('my-hello', {
        template: '<h3>{{name}}</h3>',
        data: function() { //在组件中存储数据时,必须以函数形式,函数返回一个对象
            return {
                name: 'alice'
            }
        }
    });
    new Vue({
        el: '.itany',
        components: { //局部组件,只能在当前vue实例中使用
            'my-world': {
                template: '<h3>{{age}}</h3>',
                data() {
                    return {
                        age: 25
                    }
                }
            }
        }
    })
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值