這篇文章主要介紹“VUE組件如何創(chuàng)建、渲染、及注冊(cè)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“VUE組件如何創(chuàng)建、渲染、及注冊(cè)”文章能幫助大家解決問題。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比呼倫貝爾網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式呼倫貝爾網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋呼倫貝爾地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
創(chuàng)建組件用Vue.extend()
創(chuàng)建的組件不要寫el因?yàn)樽罱K所有的組件都要被vm管理,由vm決定服務(wù)的對(duì)象
date不能寫對(duì)象形式而是寫成函數(shù)形式,且其中一定要return一個(gè)對(duì)象
// 創(chuàng)建一個(gè)組件 const school=Vue.extend({ // 配置template,里面存放的是通過(guò)當(dāng)前組件中的內(nèi)容生成的標(biāo)簽,最終會(huì)作為模板在頁(yè)面中解析出來(lái) template:` <!--最外層為template必須具有的根標(biāo)簽--> <div> <!--div中的標(biāo)簽會(huì)在頁(yè)面中使用到該組件的地方解析并顯示出來(lái),那么name就是此組件中 data中配置的name,adress同理--> <h2>我的名字:{{name}}</h2> <h3>地址:{{adress}}</h3> </div>`, // 配置date data(){ return{ name:'z', adress:'earth' } } })
注意:在全局環(huán)境下使用Vue.entend({})創(chuàng)建的組件,在不使用Vue.component('組件名',最終組件名)聲明時(shí),就是局部組件,不可以被其他vm實(shí)例使用,如果使用了Vue.component創(chuàng)建的組件時(shí)為全局組件(Vue.entend可以省略,直接const 組件名={ })
當(dāng)組件注冊(cè)完成之后,在頁(yè)面中vm服務(wù)的el內(nèi)使用,使用方式就是將最終組件名作為一個(gè)標(biāo)簽放到想要的位置等待瀏覽器解析即可
const vm=new Vue({ el:'root', components:{ // 注冊(cè)組件 sl:school } })
頁(yè)面中
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <!-- 用組件標(biāo)簽將想用的組件放到頁(yè)面上,那么組件中template屬性中的標(biāo)簽就會(huì)在這個(gè)位置被解析出來(lái)--> <sl></sl> </div> </body> </html>
總結(jié):vm實(shí)例相當(dāng)于起了一個(gè)橋梁作用,使得組件中的內(nèi)容最終會(huì)被展現(xiàn)在頁(yè)面上,這樣提高了代碼的利用率,當(dāng)我們需要重復(fù)使用某一個(gè)內(nèi)容時(shí),可以直接找到這個(gè)內(nèi)容所在的組件,然后將組件注冊(cè)到實(shí)例上即可在頁(yè)面中展示。
1、全局注冊(cè):當(dāng)Vue創(chuàng)建,組件就會(huì)被加載,不管該組件使不使用,都會(huì)被加載【占內(nèi)存】【入門程序】
2、局部注冊(cè):當(dāng)我們使用到某個(gè)組件,該組件才會(huì)被創(chuàng)建,如果不使用該組件,那么該組件不會(huì)被創(chuàng)建
創(chuàng)建vm,配置el(決定components中的組件為誰(shuí)服務(wù))
配置components對(duì)象,其中元素以key,value存儲(chǔ),key是在當(dāng)前實(shí)例中的組件名(最終組件名),value是我們?cè)谌汁h(huán)境下配置的組件名
全局注冊(cè)
Vue.component("button-a",{ template:"\n" + " <button @click=\"count++\">{{count}}</button>", data:function () { return{ count:0 } }, methods:{ } })
局部注冊(cè)
let componentA = { template:"\n" + " <button @click=\"count++\">{{count}}</button>", data:function () { return{ count:0 } }, methods:{ } } new Vue({ el:"#app", components:{ "component-a":componentA, } })
關(guān)于data為什么是一個(gè)函數(shù)
在組件中編寫html結(jié)構(gòu)時(shí),來(lái)回復(fù)制黏貼,非常麻煩,也很占內(nèi)存。解決辦法是在html里用模板編寫,并且和js分離
1、模板需要寫在template標(biāo)簽中,template標(biāo)簽寫在容器外部
2、在template標(biāo)簽中只能有一個(gè)根標(biāo)簽
<body> <div id="app"> <button-color></button-color> </div> <template id="cId"> <div> <h2>組件</h2> <h3>阿薩德</h3> </div> </template> </body> </html> <script> let ButtonColor = { template:"#cId", data:function () { } } new Vue({ el:"#app", components:{ ButtonColor } }) </script>
在html中有一些父子標(biāo)簽,在父標(biāo)簽中只能有特定的子標(biāo)簽【嚴(yán)格規(guī)范】【table、ul、ol、dl.....】,如果把子標(biāo)簽封裝成組件,在父標(biāo)簽中通過(guò)傳統(tǒng)方式使用組件,那么會(huì)出現(xiàn)顯示的效果問題,我們需要通過(guò)is進(jìn)行使用組件
<body> <div id="app"> <table> <thead> <tr> <th>序號(hào)</th> <th>姓名</th> <th>年齡</th> </tr> </thead> <tbody is="ButtonTable"> </tbody> <tfoot> <tr> <td colspan="3">尾部</td> </tr> </tfoot> </table> </div> <template id="tId"> <tbody> <tr> <td>1</td> <td>阿斯頓</td> <td>26</td> </tr> </tbody> </template> <script> let ButtonTable = { template:"#tId", } new Vue({ el:"#app", components:{ ButtonTable } }) </script> </body>
在一個(gè)組件中使用另一個(gè)組件
<body> <div id="app"> <base-color></base-color> </div> <template id="color"> <div> <h2>這是父組件</h2> <base-colo1></base-colo1> </div> </template> <template id="color1"> <h2 style="color: red">這是子組件</h2> </template> </body> </html> <script> // //創(chuàng)建子組件 let BaseColo1 ={ template:"#color1", } //創(chuàng)建父組件 let BaseColor ={ template:"#color", components:{ BaseColo1 } } new Vue({ el:"#app", components:{ BaseColor } }) </script>
1.聲明屬性
在子組件中通過(guò)聲明props屬性來(lái)接收數(shù)據(jù)
// //創(chuàng)建子組件 let BaseColo1 ={ template:"#color1", props:["msg"] }
2.傳遞數(shù)據(jù)
在父組件中使用子組件時(shí),使用v-bind進(jìn)行傳遞數(shù)據(jù)
<div> <base-colo1 :msg = "message"></base-colo1> </div>
3.在子組件中使用接收到的數(shù)據(jù)
<h3>{{msg}}</h3>
關(guān)于“VUE組件如何創(chuàng)建、渲染、及注冊(cè)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
網(wǎng)站題目:VUE組件如何創(chuàng)建、渲染、及注冊(cè)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article36/ppicsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、外貿(mào)建站、定制網(wǎng)站、小程序開發(fā)、App開發(fā)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)