vue怎么樣創建組件呢??


我知道vue中核心就是組件,但是組件是什么呢?組件有什么用呢?怎么用組件呢?怎么樣創建自己的組件呢?

前面兩個問題就不說了,這里來說說,后面的兩個問題:

1)創建自己的組件

通過vue.extend("template");通過vue構造器去拓展一個模板,然后注冊,最后使用。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>進一步了解component的話法糖</title>
        <script src="../vue.js"></script>
    </head>
    <body>
   <div>這是一個全局注冊的</div> <div id="app"> <parent></parent> </div> </body> </html> <script>
var child= Vue.extend({template:'<p>this is child component</p>'}); //Vue.component("組件名",{});在注冊之前會自動創建(調用vue.extend()方法 ) //這是一個全局注冊(兼創建與注冊) Vue.component("parent",{//有時候我們可以省略,extend. template:'<div>this is parent component ------ {{text}} <child-component></child-component> </div>',//局部使用 components:{ "child-component":child,//局部注冊 }, //data:function(){} data(){ return {text:'哈哈哈,我是組件內部的text'} }, }); var vm= new Vue({ el:'#app', data:{}, });

進階一下:(組件內部在套組件,(components下面的components))

通過下面的例子,我就想說明一點,組件是vue構造器的拓展,所以組件可能擁有構造器的幾乎所有屬性(在寫這篇博客前,我們沒有聽到這個說法,所以可能是錯的,不要信

<body>
    <div>這是一個局部注冊</div>
    <div id="app1">
            <div><button  v-on:click=get>這里觸發get方法</button></div>
        <parent-components></parent-components>
        
    </div>
</body> 

<script>    
//    var child=Vue.extend({template:"<span> ------child element------</span>"});
    var vp=new Vue({
        el:'#app1',
        data:{},
        methods:{
            get:function(){alert("這是構造器中get(全局)");}
        },
        components:{
            "parent-components":{
                template:"<div>---------<span>    parent components</span><p><button v-on:click=get>點擊觸發parent的get</button></p> <div><child-component></child-component></div>--------</div>",
                components:{
                    //局部注冊再一次局部注冊
                    "child-component":{
                        template:"<span> ------child <button v-on:click=get>點擊觸發child中的get方法</button>element------</span>",
                        methods:{
                            get:function(){
                                alert("這是局部注冊child-component組件中get");
                            }
                        }
                    }
                },
     
                methods:{
                    get:function(){
                        alert("這是蟬聯注冊parent-components里面的方法");
                    }
                },
            },
        },    

    });
    
</script>   

 

你知道嗎?一個components中可以定義多個組件:

將html,寫入components是不是覺得很low呢?當template的內容太多了,是不是不堪入目呢?那我們來使用一下vue組件的語法糖吧(不知道為啥叫這個名)

值得提醒你的事組件中的data屬性要定義成一個函數,返回一個對象,

   <script type="text/x-template" id="myComponent">
        <div>    
         <span>{{msg}}</span>
        </div>
   </script>
     
   <template id='myCom'>
      <span>{{msg}}</span>
    </template>

    <div id="app">
        <parent-component-script></parent-component-script>
    
        <parent-component-tem></parent-component-tem>
     </div>

var vm=new Vue({
        el:"#app",
        data:{},
        components:{
            "parent-component-script":{
                    template:'#myComponent',
                    data(){return{msg:'這里是script'};},
            },
            
            "parent-component-tem":{
                template:'#myCom',
                data(){return{msg:'這里是template'} }
            },
            
        },
    });

 

你也可以更狠一點:的創建方式

值得注意的是:組件中的props中屬性值,定義時是駝峰,使用時就要變為中划線

<div id="app">
    <son :son-counter="counter"></son>
    <p>parent:<input type="text"  v-model="counter"/></p>
</div>    

const son={
        template:`<div>son:<input  v-model="sonCounter"  /></div>`,
        props:{sonCounter:Number},
 };
    
var app=new Vue({
     el:'#app',
     data:{
         counter:0,
      },
     components:{
         son
     }
 
 });
    

 最后一個提醒:組件的創建要在,vue實例化之前。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM