学习vue第十四节,创建组件方法

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="lib/vue-2.4.0.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        
        <div id="app">
            
            <my-Com1></my-Com1>
            <mycom2></mycom2>
            <mycom3></mycom3>
            <mycom4></mycom4>
        </div>
        
        <!-- 在控制范围外使用template元素 -->
        <template id="temp">
            <div>
                <h3>通过id在定义组件的HTML模板</h3>
            </div>
        </template>
        
        <script type="text/javascript">
            //组件标签必须只有一个的根元素
            
                // 1.1 使用 Vue.extend 来创建全局的Vue组件
                var com1 = Vue.extend({
                  template: '<h3>这是使用 Vue.extend 创建的组件方法一</h3>' // 通过 template 属性,指定了组件要展示的HTML结构
                })
                // 1.2 使用 Vue.component('组件的名称', 创建出来的组件模板对象)
                Vue.component('myCom1', com1)
                // 如果使用 Vue.component 定义全局组件的时候,组件名称使用了 驼峰命名,则在引用组件的时候,需要把 大写的驼峰改为小写的字母,同时,两个单词之前,使用 - 链接;
                // 如果不使用驼峰,则直接拿名称来使用即可;
                // Vue.component('mycom1', com1)
            
                // Vue.component 第一个参数:组件的名称,将来在引用组件的时候,就是一个 标签形式 来引入 它的
                // 2 、第二个参数: Vue.extend 创建的组件  ,其中 template 就是组件将来要展示的HTML内容
                Vue.component('mycom2', Vue.extend({
                  template: '<h3>这是使用 Vue.extend 创建的组件方法一的一步完成</h3>'
                }))
                // 3、使用对象方式创建组件,必须用唯一的根元素
                Vue.component("mycom3",{
                    template:'<h3>直接使用component 创建组件</h3>'
                })
                // 4、使用对象方式创建组件,必须用唯一的根元素
                Vue.component("mycom4",{
                    template:'#temp'
                })

            var vm=new Vue({
                el:'#app',
                data:{
                    
                },
                methods:{
                    
                }
                
            })
            
            
        </script>
    </body>
</html>

 

全局组件与私有组件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="lib/vue-2.4.0.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        
        <div id="app">
            
            
            <mycom></mycom>
            <mycom2></mycom2>
        </div>
        
        <!-- 在控制范围外使用template元素 -->
        <template id="temp">
            <div>
                <h3>通过id在定义组件的HTML模板</h3>
            </div>
        </template>
        
        <script type="text/javascript">
            
            var vm=new Vue({
                el:'#app',
                data:{
                    
                },
                methods:{
                    
                },
                components:{//私有的组件
                    mycom:{
                        template:'<h3>我是一个私有的组件!</h3>'
                    },
                    mycom2:{
                        template:'#temp'
                    }
                    
                }
                
            })
            
            
        </script>
    </body>
</html>

 

组件进阶

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="lib/vue-2.4.0.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        
        <div id="app">
            
            <mycom1></mycom1>
            <mycom></mycom>
            <mycom1></mycom1>
        </div>
        
        <!-- 在控制范围外使用template元素 -->
        <template id="temp">
            <div>
                <h3>通过id在定义{{msg}}组件的HTML模板</h3>
            </div>
        </template>
        <template id="temp1">
            <div>
                <button type="button" @click="count">count</button>
                <h3>{{num}}</h3>
            </div>
        </template>
        <script type="text/javascript">
            // 主件可以有data数据,但是data必须是一个方法,方法内部还得返回一个对象
                Vue.component("mycom",{
                    template:'#temp',
                    data:function(){
                        return {
                            msg:'----我是全局主件的data值-------'
                        }
                    }                
                })
                // 属性写在方法好处在于,成为一个局部变量主件就算重复使用,也互不影响
                Vue.component("mycom1",{
                    template:'#temp1',
                    data:function(){
                        return {
                            num:1
                        }
                    },
                    methods:{
                        count(){
                            this.num++
                        }
                        
                    }
                })
            var vm=new Vue({
                el:'#app',
                data:{
                    
                },
                methods:{
                    
                }
                
            })
            
            
        </script>
    </body>
</html>

 

posted @ 2020-06-02 18:46  三线码工  阅读(314)  评论(0编辑  收藏  举报