关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

Vue 组件开发

发布时间:2020-04-02 00:00:00

 

将页面拆分为多个组件,简化了页面开发,方便维护,组件也可以复用。

 

组件的类型

  • 通用组件,比如表单、弹窗、菜单栏、分页组件等
  • 业务组件,实现某一业务的组件,比如抽奖组件
  • 页面组件,也叫做单页,一个页面就是一个组件,只完成功能,不复用

 

组件开发流程:声明、注册、使用

 

 

demo  组件使用流程

   <div id="app">div>
    <script>var myHeader={  //声明一个组件            template:'this is the header area'}        var myBody={
            template:'this is the body area'}        var myFooter={
            template:'this is the footer area'}        new Vue({
            el:'#app',
            components:{  //注册组件                myHeader,
                myBody,
                myFooter
            },
            template:'' //使用组件        });        script>

声明是全局声明,但需要在每一个使用Vue对象中进行注册。

 

 

使用组件有2种方式

  •   直接以变量名作为标签名
  •   单词都转换为全小写,-连接

 

 

声明组件时是用了语法糖的

    // 原来的写法var myHeader=Vue.extend({
        template:'this is the header area'})    // 语法糖var myHeader={ 
        template:'this is the header area'}

效果都一样,但使用语法糖明显要简便些

 

 

 

组件声明、注册的另一种方式

  // 声明+注册一个组件Vue.component('MyHeader',{
        template:'this is the header area'})
    
    Vue.component('MyBody',{
        template:'this is the body area'})
    
    Vue.component('MyFooter',{
        template:'this is the footer area'})        new Vue({
        el:'#app',
        template:'' //使用组件});

声明、注册都是全局的,在Vue对象中可以直接使用

 

 

组件中除了template,还可以有其它部分,比如data。

 


/template/Home/Zkeys/PC/Static