vue 注冊組件的使用詳解
一、介紹
組件系統(tǒng)是Vue.js其中一個重要的概念,它提供了一種抽象,讓我們可以使用獨(dú)立可復(fù)用的小組件來構(gòu)建大型應(yīng)用,任意類型的應(yīng)用界面都可以抽象為一個組件樹

那么什么是組件呢?
組件可以擴(kuò)展HTML元素,封裝可重用的HTML代碼,我們可以將組件看作自定義的HTML元素。
二、如何注冊組件
Vue.js的組件的使用有3個步驟:創(chuàng)建組件構(gòu)造器、注冊組件和使用組件。

下面用代碼演示這三步
<!DOCTYPE html>
<html>
<body>
<div id="app">
<!-- 注意: #app是Vue實例掛載的元素,應(yīng)該在掛載元素范圍內(nèi)使用組件-->
<my-component></my-component>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
<!-- 1.創(chuàng)建一個組件構(gòu)造器 -->
var myComponent = Vue.extend({
template: '<div>This is my first component!</div>'
})
<!-- 2.注冊組件,并指定組件的標(biāo)簽,組件的HTML標(biāo)簽為<my-component> -->
Vue.component('my-component', myComponent)
<!-- 3.通過id=app進(jìn)行掛載 -->
new Vue({
el: '#app'
});
</script>
</html>
運(yùn)行結(jié)果如下:

一、 全局注冊和局部注冊
調(diào)用Vue.component()注冊組件時,組件的注冊是全局的,這意味著該組件可以在任意Vue示例下使用。
如果不需要全局注冊,或者是讓組件使用在其它組件內(nèi),可以用選項對象的components屬性實現(xiàn)局部注冊。
我自己的理解只要是component就代表全局組件,components代表局部組件
上面的示例可以改為局部注冊的方式:
<!DOCTYPE html>
<html>
<body>
<div id="app">
<!-- 3. my-component只能在#app下使用-->
<my-component></my-component>
</div>
</body>
<script src="js/vue.js"></script>
<script>
// 1.創(chuàng)建一個組件構(gòu)造器
var myComponent = Vue.extend({
template: '<div>This is my first component!</div>'
})
new Vue({
el: '#app',
components: {
// 2. 將myComponent組件注冊到Vue實例下
'my-component' : myComponent
}
});
</script>
</html>
由于my-component組件是注冊在#app元素對應(yīng)的Vue實例下的,所以它不能在其它Vue實例下使用。
<div id="app2">
<!-- 不能使用my-component組件,因為my-component是一個局部組件,它屬于#app-->
<my-component></my-component>
</div>
<script>
new Vue({
el: '#app2'
});
</script>
二、組件注冊語法糖
以上組件注冊的方式有些繁瑣,Vue.js為了簡化這個過程,提供了注冊語法糖
// 全局注冊,my-component1是標(biāo)簽名稱
Vue.component('my-component1',{
template: '<div>This is the first component!</div>'
})
var vm1 = new Vue({
el: '#app1'
})
Vue.component()的第1個參數(shù)是標(biāo)簽名稱,第2個參數(shù)是一個選項對象,使用選項對象的template屬性定義組件模板。
使用這種方式,Vue在背后會自動地調(diào)用Vue.extend()。
components實現(xiàn)局部注冊
var vm2 = new Vue({
el: '#app2',
components: {
// 局部注冊,my-component2是標(biāo)簽名稱
'my-component2': {
template: '<div>This is the second component!</div>'
},
// 局部注冊,my-component3是標(biāo)簽名稱
'my-component3': {
template: '<div>This is the third component!</div>'
}
}
}
三、父組件和子組件
我們可以在組件中定義并使用其他組件,這就構(gòu)成了父子組件的關(guān)系。
<!DOCTYPE html>
<html>
<body>
<div id="app">
<parent-component>
</parent-component>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var Child = Vue.extend({
template: '<p>This is a child component!</p>'
})
var Parent = Vue.extend({
// 在Parent組件內(nèi)使用<child-component>標(biāo)簽
template :'<p>This is a Parent component</p><child-component></child-component>',
components: {
// 局部注冊Child組件,該組件只能在Parent組件內(nèi)使用
'child-component': Child
}
})
// 全局注冊Parent組件
Vue.component('parent-component', Parent)
new Vue({
el: '#app'
})
</script>
</html>
這段代碼的運(yùn)行結(jié)果如下

四、使用script或template標(biāo)簽
盡管語法糖簡化了組件注冊,但在template選項中拼接HTML元素比較麻煩,這也導(dǎo)致了HTML和JavaScript的高耦合性。
慶幸的是,Vue.js提供了兩種方式將定義在JavaScript中的HTML模板分離出來。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue組件</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app1">
<my-com></my-com>
<my-com1></my-com1>
</div>
<template id="myCom">
<div>這是template標(biāo)簽構(gòu)建的組件</div>
</template>
<script type="text/x-template" id="myCom1">
<div>這是script標(biāo)簽構(gòu)建的組件</div>
</script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('my-com1', {
template: '#myCom1'
});
var app1 = new Vue({
el: '#app1',
components: {
'my-com': {
template: '#myCom'
}
}
});
</script>
</body>
</html>
運(yùn)行結(jié)果:

注意:使用<script>標(biāo)簽時,type指定為text/x-template,意在告訴瀏覽器這不是一段js腳本,瀏覽器在解析HTML文檔時會忽略<script>標(biāo)簽內(nèi)定義的內(nèi)容。

在理解了組件的創(chuàng)建和注冊過程后,我建議使用<script>或<template>標(biāo)簽來定義組件的HTML模板。
這使得HTML代碼和JavaScript代碼是分離的,便于閱讀和維護(hù)。
五、模板的注意事項
1. 以子標(biāo)簽的形式在父組件中使用
<div id="app"> <parent-component> <child-component></child-component> </parent-component> </div>
上面是錯誤的。為什么這種方式無效呢?因為當(dāng)子組件注冊到父組件時,Vue.js會編譯好父組件的模板,模板的內(nèi)容已經(jīng)決定了父組件將要渲染的HTML。
<parent-component>…</parent-component>相當(dāng)于運(yùn)行時,它的一些子標(biāo)簽只會被當(dāng)作普通的HTML來執(zhí)行,<child-component></child-component>不是標(biāo)準(zhǔn)的HTML標(biāo)簽,會被瀏覽器直接忽視掉
2.組件的模板只能有一個根元素。下面的情況是不允許的。
template: `<div>這是一個局部的自定義組件,只能在當(dāng)前Vue實例中使用</div>
<button>hello</button>`
3.組件中的data必須是函數(shù)
注冊組件時傳入的配置和創(chuàng)建Vue實例差不多,但也有不同,其中一個就是data屬性必須是一個函數(shù)。
這是因為如果像Vue實例那樣,傳入一個對象,由于JS中對象類型的變量實際上保存的是對象的引用,所以當(dāng)存在多個這樣的組件時,會共享數(shù)據(jù),導(dǎo)致一個組件中數(shù)據(jù)的改變會引起其他組件數(shù)據(jù)的改變。
而使用一個返回對象的函數(shù),每次使用組件都會創(chuàng)建一個新的對象,這樣就不會出現(xiàn)共享數(shù)據(jù)的問題來了。
4.關(guān)于DOM模板的解析
當(dāng)使用 DOM 作為模版時 (例如,將 el 選項掛載到一個已存在的元素上), 你會受到 HTML 的一些限制,因為 Vue 只有在瀏覽器解析和標(biāo)準(zhǔn)化 HTML 后才能獲取模板內(nèi)容。尤其像這些元素 <ul>,<ol>,<table>,<select> 限制了能被它包裹的元素,而一些像 <option> 這樣的元素只能出現(xiàn)在某些其它元素內(nèi)部
在自定義組件中使用這些受限制的元素時會導(dǎo)致一些問題,例如
<table> <my-row>...</my-row> </table>
自定義組件 <my-row> 被認(rèn)為是無效的內(nèi)容,因此在渲染的時候會導(dǎo)致錯誤。這時應(yīng)使用特殊的 is 屬性:
<table> <tr is="my-row"></tr> </table>
也就是說,標(biāo)準(zhǔn)HTML中,一些元素中只能放置特定的子元素,另一些元素只能存在于特定的父元素中。比如table中不能放置div,tr的父元素不能div等。所以,當(dāng)使用自定義標(biāo)簽時,標(biāo)簽名還是那些標(biāo)簽的名字,但是可以在標(biāo)簽的is屬性中填寫自定義組件的名字。
三、動態(tài)組件
有的時候,在不同組件之間進(jìn)行動態(tài)切換是非常有用的,比如在一個多標(biāo)簽的界面里
簡單點(diǎn)說:就是幾個組件放在一個掛載點(diǎn)下,然后根據(jù)父組件的某個變量來決定顯示哪個,或者都不顯示。
要點(diǎn):在掛載點(diǎn)使用component標(biāo)簽,然后使用v-bind:is=”組件名”,會自動去找匹配的組件名,如果沒有,則不顯示
動態(tài)組件,先看案例效果:
代碼演示:css代碼就不復(fù)制了,上面案例效果里有。
<script src="https://unpkg.com/vue"></script>
<div id="dynamic-component-demo" class="demo">
<button v-for="tab in tabs"
v-bind:key="tab"
v-bind:class="['tab-button', { active: currentTab === tab }]"
v-on:click="currentTab = tab">{{ tab }}</button>
<component v-bind:is="currentTabComponent" class="tab"></component>
</div>
這里v-bind:key其實可有可無,具體key介紹可以看官網(wǎng)。
這里v-bind:class和v-on:click都是用來為了改變樣式用的。
關(guān)鍵是component組件標(biāo)簽。
<script>
//顯示定義了三個組件
Vue.component('tab-科長', {
template: '<div>一共有100個科長</div>'
})
Vue.component('tab-處長', {
template: '<div>一種有50個處長</div>'
})
Vue.component('tab-局長', {
template: '<div>一共有10個局長</div>'
})
new Vue({
el: '#dynamic-component-demo',
data: {
currentTab: '局長',
tabs: ['科長', '處長', '局長']
},
//計算屬性,根據(jù)currentTab的改變來判斷選擇哪個組件
computed: {
currentTabComponent: function() {
return 'tab-' + this.currentTab
}
}
})
</script>
總結(jié)
以上所述是小編給大家介紹的vue 注冊組件的使用詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
簡易Vue評論框架的實現(xiàn)(父組件的實現(xiàn))
本篇文章主要介紹了簡易 Vue 評論框架的實現(xiàn)(父組件的實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
nuxt.js服務(wù)端渲染中axios和proxy代理的配置操作
這篇文章主要介紹了nuxt.js服務(wù)端渲染中axios和proxy代理的配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue + el-form 實現(xiàn)的多層循環(huán)表單驗證
這篇文章主要介紹了vue + el-form 實現(xiàn)的多層循環(huán)表單驗證,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下。2020-11-11

