vue中的组件

1、什么是组件?

实现局部代码交互和功能的集合

组件就是一个可复用的vue实例

2、组件怎么写?

注册一个全局组件

<div id="app">
      <tab-button></tab-button>
    </div>
注册一个全局组件
        Vue.component('tab-button',{
            data:function(){
                return{
                    count : 0
                }
            },
            template:`  <button @click="count++">You clicked me {{ count }} times</button>`
        });
       new Vue({
            el:'#app',
            data:{

            },
            components:{
               tab-button
            }
        })

注册一个局部组件

<div id="app">
       <abc></abc>
    </div>
var abc = Vue.extend({
            data:function(){
                return{
                    count : 0
                }
            },
            template:` <div> <button @click="count++">You clicked me {{ count }} times</button> </div>`
        })


      new Vue({
            el:'#app',
            data:{

            },
            components:{
                abc
            }
        })

3、组件的嵌套

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="vue.js"></script>
<body>
    <div id="app">
        <app></app>
    </div>
    <script>
        var student =Vue.extend({
            data(){
                return{
                    name:'邓',
                    age:18,
                }
            },
            template:`<div>
           <h3>{{name}}</h3>
            <h3>{{age}}</h3>
        </div>`
        })
        var teacher =Vue.extend({
            data(){
                return{
                    name:'王',
                    subject:"vue课程",
                }
            },
            template:`<div>
            <h3>{{name}}</h3>
            <h3>{{subject}}</h3>
        </div>`
        })
        var school = Vue.extend({
            data(){
                return{
                    name:"北京大学",
                    adress:"北京",
                }
            },
            components:{
                 student,
                 teacher
            },
            template:`
            <div>
            <h1>{{name}}</h1>
            <h1>{{adress}}</h1>
            <student></student>
            <teacher></teacher>
        </div>`
        })
        var app = Vue.extend({
            data(){
                return{

                }
            },
            components:{
                school
            },
            template: `<div>
            <school></school>
        </div>`
        })
        new Vue({
            el:'#app',
            data:{

            },
            components:{
                app
            },
            
        })
    </script>
</body>
</html>

4、注意事项

1、组件中与new vue接收相同的属性例如data、methods、compted等,除了el

2、在组件中data必须写成函数形式,

3、多个组件被复用时,每一个不同的组件维护各自的数据

4、每使用一次组件返回的都是全新的Vuecomponent

5、组件的本质其实是一个叫Vuecomponent的构造函数

6、组件的配置中this的指向时Vuecomponent实例对象

7、组件的实例对象可以访问到vue原型上的属性、方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
   <div id="app">
    <school></school>
    <hello></hello>

    <button @click="myfunction">点击生成看看是否是一个Vuecomponent构造函数</button><br/>
    <button @click="myfunction1">点击看看是否生成的vuecomponent是否是一个</button><br/>
   
   </div> 
   <script>
       //vm:vue的实例对象
       //vc:组件实例对象(Vuecomponent的实例对象)
       var school = Vue.extend({
           data() {
               return {
                   name:"清华大学",
                   adress:"北京"
               }
           },
           template:`
           <div>
           <h1>{{name}}</h1>
           <h1>{{adress}}</h1>
           </div>
           `
       })
       var hello = Vue.extend({
           data() {
               return {
                   name:"北京大学",
                   adress:"北京"
               }
           },
           methods:{
               //在组件中,this的指向都是Vuecomponent的实例对象,
               //并且结构和vue实例对象的结构一模一样,但是vue实例对象可以包含多个Vuecomponent
               myfunction2(){
                   console.log(this)
               }
           },
           template:`
           <div>
           <h1>{{name}}</h1>
           <h1>{{adress}}</h1>
           <button @click="myfunction2">点击查看this的指向</button><br/>
           </div>
           `
       })
       new Vue({
           el:'#app',
           data:{

           },
           methods:{
               //组件本身就是一个Vuecomponent构造函数,时Vue.extend()生成的
               myfunction(){
                console.log(school)
               },
               //每次调用Vue.extend函数返回的都是全新的Vuecomponent构造函数
               myfunction1(){
                   console.log(school === hello)
               },
               
           },
           components:{
            school,
            hello
           }
       })
   </script>
</body>
</html>

实现页面

验证效果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值