<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件的分类</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <my-hello></my-hello>
        <my-world></my-world>
    </div>

    <script>
        /**
         * 全局组件,可以在所有vue实例中使用
         */
        Vue.component('my-hello',{
            template:'<h3>{{name}}</h3>',
            data:function(){ //在组件中存储数据时,必须以函数形式,函数返回一个对象
                return {
                    name:'alice'
                }
            }
        });

        /**
         * 局部组件,只能在当前vue实例中使用
         */
        var vm=new Vue({
            el:'#itany',
            data:{
                name:'tom'
            },
            components:{ //局部组件
                'my-world':{
                    template:'<h3>{{age}}</h3>',
                    data(){
                        return {
                            age:25
                        }
                    }
                }
            }
        });    
    </script>
</body>
</html>

 

相关文章:

  • 2022-12-23
  • 2021-04-18
  • 2021-07-04
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2021-11-06
  • 2021-12-02
猜你喜欢
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2021-09-22
  • 2021-12-04
  • 2022-12-23
相关资源
相似解决方案