首页 > 其他 > 详细

Vue 组件&组件之间的通信 之组件的介绍

时间:2019-04-23 16:25:43      阅读:124      评论:0      收藏:0      [点我收藏+]

什么是组件?

技术分享图片

 

组件Component,可扩展HTML元素,封装可重用的代码。通俗的来说,组件将可重用的HTML元素封装成为标签方便复用;

组件的使用:

1、使用全局的方法Vue.extend创建构造器;

2、再使用全局的方法Vue.component注册组件;

注意:在Vue.component里需要指明组件的名称,组件名称不能使用内置标签名称,如body.推荐使用短横线命名规则。

如:

my-component    正确 (推荐使用)

my-Component   错误 

mycomponent    正确 

Mycomponent    正确 

myComponent   错误 

MyComponent   错误 

 

示例:

技术分享图片

 

vue 代码:

<script>
            window.onload= () =>{
                
                
                
                //创建构造器
                let myComponent=Vue.extend({
                    
                    template:"<h1>欢迎来到perfect*的博客园</h1>"
                });
                
                //注册组件
                Vue.component(my-component,myComponent);
                
                
          new Vue({
                el:div,
                data:{
                    
                    
                }
                
                
                
                
                
            })}
        </script>

html:

<div>
            <my-component></my-component>
           
            
            
        </div>

 

使用注册组件简写的方式:

出现的问题:

技术分享图片

 

 技术分享图片

修改后的效果:

技术分享图片

 

 

 vue代码:

//注册组件的简写方式
                
                Vue.component(my-componenta,{
                    
                    template:<h2>hello vue</h2>
                });
                

html:

<my-componentA></my-componentA>

html标签是大小写不分的

组件的命名不支持驼峰命名方式

 最终所有代码:

技术分享图片
 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title>component</title>
 6         <script type="text/javascript" src="../js/vue.js" ></script>
 7         
 8         <script>
 9             window.onload= () =>{
10                 
11                 
12                 
13                 //创建构造器
14                 let myComponent=Vue.extend({
15                     
16                     template:"<h1>欢迎来到perfect*的博客园</h1>"
17                 });
18                 
19                 //注册组件
20                 Vue.component(my-component,myComponent);
21                 
22                 
23                 
24                 
25                 //注册组件的简写方式
26                 
27                 Vue.component(my-componenta,{
28                     
29                     
30                     template:<h2>hello vue</h2>
31                 });
32                 
33                 
34                 
35                 new Vue({
36                 el:div,
37                 data:{
38                     
39                     
40                 }
41                 
42                 
43                 
44                 
45                 
46             })}
47         </script>
48     </head>
49     <body>
50         <div>
51             <my-component></my-component>
52             <my-componentA></my-componentA>
53            
54             
55             
56         </div>
57     </body>
58 </html>
组件的介绍

 

 详细介绍见官网:https://cn.vuejs.org/v2/api/#%E5%85%A8%E5%B1%80-API

 

Vue 组件&组件之间的通信 之组件的介绍

原文:https://www.cnblogs.com/jiguiyan/p/10756887.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!