組件簡(jiǎn)介
組件系統(tǒng)是Vue.js其中一個(gè)重要的概念春宣,它提供了一種抽象,讓我們可以使用獨(dú)立可復(fù)用的小組件來(lái)構(gòu)建大型應(yīng)用荣倾,任意類(lèi)型的應(yīng)用界面都可以抽象為一個(gè)組件樹(shù):
那么什么是組件呢腿椎?
組件可以擴(kuò)展HTML元素,封裝可重用的HTML代碼蝉揍,我們可以將組件看作自定義的HTML元素。
本文的Demo和源代碼已放到GitHub畦娄,如果您覺(jué)得本篇內(nèi)容不錯(cuò)又沾,請(qǐng)?jiān)贕itHub上加個(gè)星星!
(所有示例都放在GitHub Pages上了熙卡,請(qǐng)?jiān)L問(wèn)https://github.com/keepfool/vue-tutorials查看示例匯總)
由于組件的篇幅較大杖刷,我將會(huì)把組件的入門(mén)知識(shí)分為兩篇來(lái)講解,這樣也便于各位看官們快速消化再膳。
組件的創(chuàng)建和注冊(cè)
基本步驟
Vue.js的組件的使用有3個(gè)步驟:創(chuàng)建組件構(gòu)造器挺勿、注冊(cè)組件和使用組件。
下面的代碼演示了這3個(gè)步驟:
<!DOCTYPE html>
<html>
<body> <div id="app"> <!-- 3\. #app是Vue實(shí)例掛載的元素喂柒,應(yīng)該在掛載元素范圍內(nèi)使用組件--> <my-component></my-component> </div> </body> <script src="js/vue.js"></script> <script> // 1.創(chuàng)建一個(gè)組件構(gòu)造器 var myComponent = Vue.extend({ template: '<div>This is my first component!</div>' }) // 2.注冊(cè)組件,并指定組件的標(biāo)簽禾嫉,組件的HTML標(biāo)簽為<my-component> Vue.component('my-component', myComponent) new Vue({ el: '#app' }); </script>
</html>
運(yùn)行結(jié)果如下:
可以看到灾杰,使用組件和使用普通的HTML元素沒(méi)什么區(qū)別。
理解組件的創(chuàng)建和注冊(cè)
我們用以下幾個(gè)步驟來(lái)理解組件的創(chuàng)建和注冊(cè):
1. Vue.extend()是Vue構(gòu)造器的擴(kuò)展熙参,調(diào)用Vue.extend()創(chuàng)建的是一個(gè)組件構(gòu)造器艳吠,而不是一個(gè)具體的組件實(shí)例。
2. Vue.extend()構(gòu)造器有一個(gè)選項(xiàng)對(duì)象孽椰,選項(xiàng)對(duì)象的template屬性用于定義組件要渲染的HTML昭娩。
3. 使用Vue.component()注冊(cè)組件時(shí)凛篙,需要提供2個(gè)參數(shù),第1個(gè)參數(shù)時(shí)組件的標(biāo)簽栏渺,第2個(gè)參數(shù)是組件構(gòu)造器呛梆。
4. Vue.component()方法內(nèi)部會(huì)調(diào)用組件構(gòu)造器,創(chuàng)建一個(gè)組件實(shí)例磕诊。
5. 組件應(yīng)該掛載到某個(gè)Vue實(shí)例下填物,否則它不會(huì)生效。
請(qǐng)注意第5點(diǎn)霎终,以下代碼在3個(gè)地方使用了<my-component>標(biāo)簽滞磺,但只有#app1和#app2下的<my-component>標(biāo)簽才起到作用。
<!DOCTYPE html>
<html>
<body> <div id="app1"> <my-component></my-component> </div> <div id="app2"> <my-component></my-component> </div> <!--該組件不會(huì)被渲染--> <my-component></my-component> </body> <script src="js/vue.js"></script> <script> var myComponent = Vue.extend({ template: '<div>This is a component!</div>' }) Vue.component('my-component', myComponent) var app1 = new Vue({ el: '#app1' }); var app2 = new Vue({ el: '#app2' }) </script>
</html>
全局注冊(cè)和局部注冊(cè)
調(diào)用Vue.component()注冊(cè)組件時(shí)莱褒,組件的注冊(cè)是全局的击困,這意味著該組件可以在任意Vue示例下使用。
如果不需要全局注冊(cè)广凸,或者是讓組件使用在其它組件內(nèi)阅茶,可以用選項(xiàng)對(duì)象的components屬性實(shí)現(xiàn)局部注冊(cè)。
上面的示例可以改為局部注冊(cè)的方式:
<!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è)組件構(gòu)造器 var myComponent = Vue.extend({ template: '<div>This is my first component!</div>' }) new Vue({ el: '#app', components: { // 2\. 將myComponent組件注冊(cè)到Vue實(shí)例下 'my-component' : myComponent } }); </script>
</html>
由于my-component組件是注冊(cè)在#app元素對(duì)應(yīng)的Vue實(shí)例下的炮障,所以它不能在其它Vue實(shí)例下使用目派。
<div id="app2">
<!-- 不能使用my-component組件,因?yàn)閙y-component是一個(gè)局部組件胁赢,它屬于#app--> <my-component></my-component>
</div>
<script>
new Vue({ el: '#app2' });
</script>
如果你這樣做了企蹭,瀏覽器會(huì)提示一個(gè)錯(cuò)誤:
父組件和子組件
我們可以在組件中定義并使用其他組件,這就構(gòu)成了父子組件的關(guān)系智末。
<!DOCTYPE html>
<html>
<body> <div id="app"> <parent-component> </parent-component> </div> </body> <script src="js/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: { // 局部注冊(cè)Child組件谅摄,該組件只能在Parent組件內(nèi)使用 'child-component': Child } }) // 全局注冊(cè)Parent組件 Vue.component('parent-component', Parent) new Vue({ el: '#app' }) </script>
</html>
這段代碼的運(yùn)行結(jié)果如下:
我們分幾個(gè)步驟來(lái)理解這段代碼:
var Child = Vue.extend(...)定義一了個(gè)Child組件構(gòu)造器
var Parent = Vue.extend(...)定義一個(gè)Parent組件構(gòu)造器
components: { 'child-component': Child },將Child組件注冊(cè)到Parent組件系馆,并將Child組件的標(biāo)簽設(shè)置為child-component送漠。
template :'<p>This is a Parent component</p><child-component></child-component>',在Parent組件內(nèi)以標(biāo)簽的形式使用Child組件由蘑。
Vue.component('parent-component', Parent) 全局注冊(cè)Parent組件
在頁(yè)面中使用<parent-component>標(biāo)簽渲染Parent組件的內(nèi)容闽寡,同時(shí)Child組件的內(nèi)容也被渲染出來(lái)
Child組件是在Parent組件中注冊(cè)的,它只能在Parent組件中使用尼酿,確切地說(shuō):子組件只能在父組件的template中使用爷狈。
請(qǐng)注意下面兩種子組件的使用方式是錯(cuò)誤的:
1. 以子標(biāo)簽的形式在父組件中使用
<div id="app">
<parent-component> <child-component></child-component> </parent-component>
</div>
為什么這種方式無(wú)效呢?因?yàn)楫?dāng)子組件注冊(cè)到父組件時(shí)裳擎,Vue.js會(huì)編譯好父組件的模板涎永,模板的內(nèi)容已經(jīng)決定了父組件將要渲染的HTML。
<parent-component>…</parent-component>相當(dāng)于運(yùn)行時(shí),它的一些子標(biāo)簽只會(huì)被當(dāng)作普通的HTML來(lái)執(zhí)行羡微,<child-component></child-component>不是標(biāo)準(zhǔn)的HTML標(biāo)簽谷饿,會(huì)被瀏覽器直接忽視掉。
2. 在父組件標(biāo)簽外使用子組件
<div id="app">
<parent-component> </parent-component> <child-component> </child-component>
</div>
運(yùn)行這段代碼妈倔,瀏覽器會(huì)提示以下錯(cuò)誤
組件注冊(cè)語(yǔ)法糖
以上組件注冊(cè)的方式有些繁瑣博投,Vue.js為了簡(jiǎn)化這個(gè)過(guò)程,提供了注冊(cè)語(yǔ)法糖启涯。
使用Vue.component()直接創(chuàng)建和注冊(cè)組件:
// 全局注冊(cè)贬堵,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個(gè)參數(shù)是標(biāo)簽名稱,第2個(gè)參數(shù)是一個(gè)選項(xiàng)對(duì)象结洼,使用選項(xiàng)對(duì)象的template屬性定義組件模板黎做。
使用這種方式,Vue在背后會(huì)自動(dòng)地調(diào)用Vue.extend()松忍。
在選項(xiàng)對(duì)象的components屬性中實(shí)現(xiàn)局部注冊(cè):
var vm2 = new Vue({
el: '#app2', components: { // 局部注冊(cè)蒸殿,my-component2是標(biāo)簽名稱 'my-component2': { template: '<div>This is the second component!</div>' }, // 局部注冊(cè),my-component3是標(biāo)簽名稱 'my-component3': { template: '<div>This is the third component!</div>' } }
})
使用script或template標(biāo)簽
盡管語(yǔ)法糖簡(jiǎn)化了組件注冊(cè)鸣峭,但在template選項(xiàng)中拼接HTML元素比較麻煩宏所,這也導(dǎo)致了HTML和JavaScript的高耦合性。
慶幸的是摊溶,Vue.js提供了兩種方式將定義在JavaScript中的HTML模板分離出來(lái)爬骤。
使用<script>標(biāo)簽
<!DOCTYPE html>
<html>
<body> <div id="app"> <my-component></my-component> </div> <script type="text/x-template" id="myComponent"> <div>This is a component!</div> </script> </body> <script src="js/vue.js"></script> <script> Vue.component('my-component',{ template: '#myComponent' }) new Vue({ el: '#app' }) </script>
</html>
template選項(xiàng)現(xiàn)在不再是HTML元素,而是一個(gè)id莫换,Vue.js根據(jù)這個(gè)id查找對(duì)應(yīng)的元素霞玄,然后將這個(gè)元素內(nèi)的HTML作為模板進(jìn)行編譯。
注意:使用<script>標(biāo)簽時(shí)拉岁,type指定為text/x-template坷剧,意在告訴瀏覽器這不是一段js腳本,瀏覽器在解析HTML文檔時(shí)會(huì)忽略<script>標(biāo)簽內(nèi)定義的內(nèi)容喊暖。
使用<template>標(biāo)簽
如果使用<template>標(biāo)簽惫企,則不需要指定type屬性。
<!DOCTYPE html>
<html>
<head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="app"> <my-component></my-component> </div> <template id="myComponent"> <div>This is a component!</div> </template> </body> <script src="js/vue.js"></script> <script> Vue.component('my-component',{ template: '#myComponent' }) new Vue({ el: '#app' }) </script>
</html>
在理解了組件的創(chuàng)建和注冊(cè)過(guò)程后陵叽,我建議使用<script>或<template>標(biāo)簽來(lái)定義組件的HTML模板狞尔。
這使得HTML代碼和JavaScript代碼是分離的,便于閱讀和維護(hù)巩掺。
另外沪么,在Vue.js中,可創(chuàng)建.vue后綴的文件锌半,在.vue文件中定義組件,這個(gè)內(nèi)容我會(huì)在后面的文章介紹。
組件的el和data選項(xiàng)
傳入Vue構(gòu)造器的多數(shù)選項(xiàng)也可以用在 Vue.extend() 或Vue.component()中刊殉,不過(guò)有兩個(gè)特例: data 和el殉摔。
Vue.js規(guī)定:在定義組件的選項(xiàng)時(shí),data和el選項(xiàng)必須使用函數(shù)记焊。
下面的代碼在執(zhí)行時(shí)逸月,瀏覽器會(huì)提出一個(gè)錯(cuò)誤
Vue.component('my-component', {
data: { a: 1 }
})
另外,如果data選項(xiàng)指向某個(gè)對(duì)象遍膜,這意味著所有的組件實(shí)例共用一個(gè)data碗硬。
我們應(yīng)當(dāng)使用一個(gè)函數(shù)作為 data 選項(xiàng),讓這個(gè)函數(shù)返回一個(gè)新對(duì)象:
Vue.component('my-component', {
data: function(){ return {a : 1} }
})
使用props
組件實(shí)例的作用域是孤立的瓢颅。這意味著不能并且不應(yīng)該在子組件的模板內(nèi)直接引用父組件的數(shù)據(jù)恩尾。可以使用 props 把數(shù)據(jù)傳給子組件挽懦。
props基礎(chǔ)示例
下面的代碼定義了一個(gè)子組件my-component翰意,在Vue實(shí)例中定義了data選項(xiàng)。
var vm = new Vue({
el: '#app', data: { name: 'keepfool', age: 28 }, components: { 'my-component': { template: '#myComponent', props: ['myName', 'myAge'] } }
})
為了便于理解信柿,你可以將這個(gè)Vue實(shí)例看作my-component的父組件冀偶。
如果我們想使父組件的數(shù)據(jù),則必須先在子組件中定義props屬性渔嚷,也就是props: ['myName', 'myAge']這行代碼进鸠。
定義子組件的HTML模板:
<template id="myComponent">
<table> <tr> <th colspan="2"> 子組件數(shù)據(jù) </th> </tr> <tr> <td>my name</td> <td>{{ myName }}</td> </tr> <tr> <td>my age</td> <td>{{ myAge }}</td> </tr> </table>
</template>
將父組件數(shù)據(jù)通過(guò)已定義好的props屬性傳遞給子組件:
<div id="app">
<my-component v-bind:my-name="name" v-bind:my-age="age"></my-component>
</div>
注意:在子組件中定義prop時(shí),使用了camelCase命名法形病。由于HTML特性不區(qū)分大小寫(xiě)客年,camelCase的prop用于特性時(shí),需要轉(zhuǎn)為 kebab-case(短橫線隔開(kāi))窒朋。例如搀罢,在prop中定義的myName,在用作特性時(shí)需要轉(zhuǎn)換為my-name侥猩。
這段程序的運(yùn)行結(jié)果如下:
父組件是如何將數(shù)據(jù)傳給子組件的呢榔至?相信看了下面這圖,也許你就能很好地理解了欺劳。
在父組件中使用子組件時(shí)唧取,通過(guò)以下語(yǔ)法將數(shù)據(jù)傳遞給子組件:
<child-component v-bind:子組件prop="父組件數(shù)據(jù)屬性"></child-component>
prop的綁定類(lèi)型
單向綁定
既然父組件將數(shù)據(jù)傳遞給了子組件,那么如果子組件修改了數(shù)據(jù)划提,對(duì)父組件是否會(huì)有所影響呢枫弟?
我們將子組件模板和頁(yè)面HTML稍作更改:
運(yùn)行這個(gè)頁(yè)面,我們做兩個(gè)小試驗(yàn):
1. 在頁(yè)面上修改子組件的數(shù)據(jù)
修改了子組件的數(shù)據(jù)鹏往,沒(méi)有影響父組件的數(shù)據(jù)淡诗。
2. 在頁(yè)面上修改父組件的數(shù)據(jù)
修改了父組件的數(shù)據(jù),同時(shí)影響了子組件。
prop默認(rèn)是單向綁定:當(dāng)父組件的屬性變化時(shí)韩容,將傳導(dǎo)給子組件款违,但是反過(guò)來(lái)不會(huì)。這是為了防止子組件無(wú)意修改了父組件的狀態(tài)
雙向綁定
可以使用.sync顯式地指定雙向綁定群凶,這使得子組件的數(shù)據(jù)修改會(huì)回傳給父組件插爹。
<my-component v-bind:my-name.sync="name" v-bind:my-age.sync="age"></my-component>
單次綁定
可以使用.once顯式地指定單次綁定,單次綁定在建立之后不會(huì)同步之后的變化请梢,這意味著即使父組件修改了數(shù)據(jù)赠尾,也不會(huì)傳導(dǎo)給子組件。
<my-component v-bind:my-name.once="name" v-bind:my-age.once="age"></my-component>
示例
為了盡快消化這些知識(shí)毅弧,我們來(lái)做一個(gè)小示例吧气嫁。
除了以上介紹的知識(shí)點(diǎn),這個(gè)示例還用到了兩個(gè)知識(shí)點(diǎn):
1. prop驗(yàn)證
props: {
data: Array, columns: Array, filterKey: String
}
這段代碼表示:父組件傳遞過(guò)來(lái)的data和columns必須是Array類(lèi)型形真,filterKey必須是字符串類(lèi)型杉编。
更多prop驗(yàn)證的介紹,請(qǐng)參考:官方文檔prop驗(yàn)證
2. filterBy過(guò)濾器
可以根據(jù)指定的字符串過(guò)濾數(shù)據(jù)咆霜。
總結(jié)
使用組件的前提是創(chuàng)建并注冊(cè)組件邓馒,本篇文章詳細(xì)介紹了組件從創(chuàng)建到使用的步驟,并介紹了幾種不同的方式去創(chuàng)建和注冊(cè)組件蛾坯;然后介紹了組件的props選項(xiàng)光酣,它用于將父組件的數(shù)據(jù)傳遞給子組件,最后我們用一個(gè)小的示例演示了這些知識(shí)點(diǎn)脉课。