vue 怎麼建立組件及組件使用方法,vue建立

來源:互聯網
上載者:User

vue 怎麼建立組件及組件使用方法,vue建立

什麼是組件?

組件 (Component) 是 Vue.js 最強大的功能之一。組件可以擴充 HTML 元素,封裝可重用的代碼。在較高層面上,組件是自訂元素,Vue.js 的編譯器為它添加特殊功能。在有些情況下,組件也可以是原生 HTML 元素的形式,以 is 特性擴充。

我知道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執行個體化之前。

總結

以上所述是小編給大家介紹的vue 怎麼建立組件及組件使用方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.