Vue组件基础知识总结
組件系統(tǒng)是Vue.js其中一個重要的概念,它提供了一種抽象,讓我們可以使用獨立可復(fù)用的小組件來構(gòu)建大型應(yīng)用,任意類型的應(yīng)用界面都可以抽象為一個組件樹。
那么什么是組件呢?組件可以擴(kuò)展HTML元素,封裝可重用的HTML代碼,我們可以將組件看作自定義的HTML元素。
一、組件的創(chuàng)建和注冊基本步驟
Vue.js的組件的使用有3個步驟:創(chuàng)建組件構(gòu)造器、注冊組件和使用組件。
<!DOCTYPE html> <html> <head><title></title><script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body><div id="app"><!-- 3. #app是Vue實例掛載的元素,應(yīng)該在掛載元素范圍內(nèi)使用組件--><my-component></my-component></div><script>// 1.創(chuàng)建一個組件構(gòu)造器var myComponent = Vue.extend({template: '<h1>This is my first component!</h1>'})// 2.注冊組件,并指定組件的標(biāo)簽,組件的HTML標(biāo)簽為<my-component>Vue.component('my-component', myComponent)new Vue({el: '#app'});</script> </body> </html>
上面代碼演示了這3個步驟:可以看到,使用組件和使用普通的HTML元素沒什么區(qū)別。
二、理解組件的創(chuàng)建和注冊
我們用以下幾個步驟來理解組件的創(chuàng)建和注冊:
1.?Vue.extend()是Vue構(gòu)造器的擴(kuò)展,調(diào)用Vue.extend()創(chuàng)建的是一個組件構(gòu)造器,而不是一個具體的組件實例。?2.?Vue.extend()構(gòu)造器有一個選項對象,選項對象的template屬性用于定義組件要渲染的HTML。?
3. 使用Vue.component()注冊組件時,需要提供2個參數(shù),第1個參數(shù)是組件的標(biāo)簽,第2個參數(shù)是組件構(gòu)造器。?
4.?Vue.component()方法內(nèi)部會調(diào)用組件構(gòu)造器,創(chuàng)建一個組件實例。?
5. 組件應(yīng)該掛載到某個Vue實例下,否則它不會生效。
請注意第5點,只有掛載到vue實例下組件才起到作用。
三、全局注冊和局部注冊
調(diào)用Vue.component()注冊組件時,組件的注冊是全局的,這意味著該組件可以在任意Vue實例下使用。
如果不需要全局注冊,或者是讓組件使用在其它組件內(nèi),可以用選項對象的components屬性實現(xiàn)局部注冊。
? 局部組件注冊方式:
// 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} });注意:該局部組件只能在app下才能起作用
四、父組件和子組件
我們可以在組件中定義并使用其他組件,這就構(gòu)成了父子組件的關(guān)系。
<!DOCTYPE html> <html> <head><title></title><script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body><div id="app"><!-- 3. #app是Vue實例掛載的元素,應(yīng)該在掛載元素范圍內(nèi)使用組件--><parent-component></parent-component></div><script>// 1.創(chuàng)建一個組件構(gòu)造器// 子集var child = Vue.extend({template:"<h3>child component</h3>"});//父級var parent = Vue.extend({//在parent組件里使用child組件template: '<div><h2>This is parent component!</h2><child-component></child-component></div>',//在parent里面注冊child組件,屬于一種局部注冊的方式 components:{"child-component":child}});// 2、全局注冊parent組件Vue.component("parent-component",parent);new Vue({el: '#app'});</script> </body> </html>
注意:組件的模板只能有一個根元素。下面的情況是不允許的。
template: '<h2>This is parent component!</h2><child-component></child-component>',我們分幾個步驟來理解這段代碼:
(1)var Child = Vue.extend(...)定義一了個Child組件構(gòu)造器
(2)var Parent = Vue.extend(...)定義一個Parent組件構(gòu)造器
(3)components: { 'child-component': Child },將Child組件注冊到Parent組件,并將Child組件的標(biāo)簽設(shè)置為child-component。
(4)template :'<p>This is a Parent component</p><child-component></child-component>',在Parent組件內(nèi)以標(biāo)簽的形式使用Child組件。
(5)Vue.component('parent-component', Parent)?全局注冊Parent組件
(6)在頁面中使用<parent-component>標(biāo)簽渲染Parent組件的內(nèi)容,同時Child組件的內(nèi)容也被渲染出來
Child組件是在Parent組件中局部注冊的,它只能在Parent組件中使用,確切地說:子組件只能在父組件的template中使用。
請注意下面兩種子組件的使用方式是錯誤的:
1、以子標(biāo)簽的形式在父組件中使用
2、在父組件標(biāo)簽外使用子組件
//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)于運行時,它的一些子標(biāo)簽只會被當(dāng)作普通的HTML來執(zhí)行,<child-component></child-component>不是標(biāo)準(zhǔn)的HTML標(biāo)簽,會被瀏覽器直接忽視掉。//2. 在父組件標(biāo)簽外使用子組件 <div id="app"><parent-component></parent-component><child-component></child-component> </div> //運行這段代碼,瀏覽器會提示以下錯誤:Unknown custom element: <child-component> - did you register the component correctly?
五、組件注冊語法糖
以上組件注冊的方式有些繁瑣,Vue.js為了簡化這個過程,提供了注冊語法糖:相當(dāng)于把Vue.extend()直接放入第二個參數(shù)
1、使用Vue.component()直接創(chuàng)建和注冊組件:
// 全局注冊,my-component是標(biāo)簽名稱,后面跟模板template Vue.component('my-component',{template: '<div>This is the first component!</div>' })Vue.component()的第1個參數(shù)是標(biāo)簽名稱,第2個參數(shù)是一個選項對象,使用選項對象的template屬性定義組件模板。使用這種方式,Vue在背后會自動地調(diào)用Vue.extend()。
2、在選項對象的components屬性中實現(xiàn)局部注冊:
var vm = new Vue({el: '#app',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>'}} })六、使用script或template標(biāo)簽
盡管語法糖簡化了組件注冊,但在template選項中拼接HTML元素比較麻煩,這也導(dǎo)致了HTML和JavaScript的高耦合性。慶幸的是,Vue.js提供了兩種方式將定義在JavaScript中的HTML模板分離出來。
1、使用<script>標(biāo)簽
<!DOCTYPE html> <html> <head><title></title><script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body><div id="app"><parent-component></parent-component></div><script type="text/x-template" id="template"><div>this is a template!</div></script><script>// 1.創(chuàng)建一個組件構(gòu)造器var parent = Vue.extend({//在parent組件里使用child組件template: '#template',});// 2、全局注冊parent組件Vue.component("parent-component",parent);new Vue({el: '#app'});</script> </body> </html>
template選項現(xiàn)在不再是HTML元素,而是一個id選擇器,Vue.js根據(jù)這個id查找對應(yīng)的元素,然后將這個元素內(nèi)的HTML作為模板進(jìn)行編譯。
注意:使用<script>標(biāo)簽時,type指定為text/x-template,意在告訴瀏覽器這不是一段js腳本,瀏覽器在解析HTML文檔時會忽略<script>標(biāo)簽內(nèi)定義的內(nèi)容。
2、使用<template>標(biāo)簽
如果使用<template>標(biāo)簽,則不需要指定type屬性。
//template里面必須還得跟html標(biāo)簽,不能直接寫text,否則瀏覽器會報錯<template id="template"><div>this is a template!</div></template>在理解了組件的創(chuàng)建和注冊過程后,我建議使用<script>或<template>標(biāo)簽來定義組件的HTML模板。這使得HTML代碼和JavaScript代碼是分離的,便于閱讀和維護(hù)。
七、組件的el和data選項
傳入Vue構(gòu)造器的多數(shù)選項也可以用在?Vue.extend()?或Vue.component()中,不過有兩個特例:?data?和el。
Vue.js規(guī)定:在定義組件的選項時,data和el選項必須使用函數(shù)。這是因為如果像Vue實例那樣,傳入一個對象,由于JS中對象類型的變量實際上保存的是對象的引用,所以當(dāng)存在多個這樣的組件時,會共享數(shù)據(jù),導(dǎo)致一個組件中數(shù)據(jù)的改變會引起其他組件數(shù)據(jù)的改變。而使用一個返回對象的函數(shù),每次使用組件都會創(chuàng)建一個新的對象,這樣就不會出現(xiàn)共享數(shù)據(jù)的問題來了。
下面的代碼在執(zhí)行時,瀏覽器會提出一個錯誤
Vue.component('my-component', {data: {a: 1} })
如果data選項指向某個對象,這意味著所有的組件實例共用一個data。我們應(yīng)當(dāng)使用一個函數(shù)作為 data 選項,讓這個函數(shù)返回一個新對象:
Vue.component('my-component', {data: function(){return {a : 1}} })八、父子組件通訊
組件實例的作用域是孤立的。這意味著不能并且不應(yīng)該在子組件的模板內(nèi)直接引用父組件的數(shù)據(jù)。可以使用?props?把數(shù)據(jù)傳給子組件
1、props基礎(chǔ)示例:
下面4步
<!DOCTYPE html> <html> <head><title></title><script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body><div id="app">//4、將父組件數(shù)據(jù)通過已定義好的props屬性傳遞給子組件<my-component v-bind:my-name="name" v-bind:my-age="age"></my-component></div>//3、定義子組件的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><script>var vm = new Vue({el: '#app',data: {name: 'keepfool',age: 28},//1、定義一個子組件my-component,在Vue實例中定義了data選項 components: {'my-component': {template: '#myComponent',//為了便于理解,你可以將這個Vue實例看作my-component的父組件。//2、如果我們想使用父組件的數(shù)據(jù),則必須先在子組件中定義props屬性,也就是props: ['myName', 'myAge']props: ['myName', 'myAge']}}})</script> </body> </html>注意:在子組件中定義prop時,使用了camelCase命名法。由于HTML特性不區(qū)分大小寫,camelCase的prop用于特性時,需要轉(zhuǎn)為 kebab-case(短橫線隔開)。例如,在prop中定義的myName,在用作特性時需要轉(zhuǎn)換為my-name。
簡單效果:
父組件是如何將數(shù)據(jù)傳給子組件的呢?相信看了下面這圖,也許你就能很好地理解了。
?
在父組件中使用子組件時,通過以下語法將數(shù)據(jù)傳遞給子組件:
<child-component v-bind:子組件prop="父組件數(shù)據(jù)屬性"></child-component>2、prop的綁定類型:單向綁定
既然父組件將數(shù)據(jù)傳遞給了子組件,那么如果子組件修改了數(shù)據(jù),對父組件是否會有所影響呢?我們將子組件模板和頁面HTML稍作更改:
<!DOCTYPE html> <html> <head><title></title><script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body><div id="app"><table><tr><th colspan="3">父組件數(shù)據(jù)</td></tr><tr><td>name</td><td>{{ name }}</td><td><input type="text" v-model="name" /></td></tr><tr><td>age</td><td>{{ age }}</td><td><input type="text" v-model="age" /></td></tr></table><my-component v-bind:my-name="name" v-bind:my-age="age"></my-component></div><template id="myComponent"><table><tr><th colspan="3">子組件數(shù)據(jù)</th></tr><tr><td>my name</td><td>{{ myName }}</td><td><input type="text" v-model="myName"></td></tr><tr><td>my age</td><td>{{ myAge }}</td><td><input type="text" v-model="myAge"></td></tr></table></template><script>var vm = new Vue({el: '#app',data: {name: 'keepfool',age: 28},//1、定義一個子組件my-component,在Vue實例中定義了data選項 components: {'my-component': {template: '#myComponent',//為了便于理解,你可以將這個Vue實例看作my-component的父組件。//2、如果我們想使用父組件的數(shù)據(jù),則必須先在子組件中定義props屬性,也就是props: ['myName', 'myAge']props: ['myName', 'myAge']}}})</script> </body> </html>測試驗證結(jié)果:
(1)在頁面上修改子組件的數(shù)據(jù),對父組件數(shù)據(jù)沒有影響;
(2)在頁面上修改父組件的數(shù)據(jù),同時子組件數(shù)據(jù)跟著改變。
因此可見:prop默認(rèn)是單向綁定:當(dāng)父組件的屬性變化時,將傳導(dǎo)給子組件,但是反過來不會。這是為了防止子組件無意修改了父組件的狀態(tài)。
3、雙向綁定
可以使用.sync顯式地指定雙向綁定,這使得子組件的數(shù)據(jù)修改會回傳給父組件。
<my-component v-bind:my-name.sync="name" v-bind:my-age.sync="age"></my-component>注意:只有vue1.0的可以,我使用的版本是vue.1.0.25
4、示例
為了盡快消化這些知識,我們來做一個小示例:可以直接拷貝查看效果
<!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title></head><body><div id="app"><div>Search:<input type="text" v-model="searchQuery" /></div><my-component v-bind:data="gridData" v-bind:columns="gridColumns" v-bind:filter-key="searchQuery"></my-component></div><template id="myComponent"><table><th v-for="col in columns">{{col | capitalize}}</th><tr v-for="entry in data | filterBy filterKey"><td v-for="val in entry">{{val}}</td></tr></table></template></body><script src="vue.js"></script><script>var vm = new Vue({el: '#app',data: {searchQuery: '',gridColumns: ["name","age","sex"],gridData:[{name: 'Jack',age: 30,sex: 'Male'}, {name: 'Bill',age: 26,sex: 'Male'}, {name: 'Tracy',age: 22,sex: 'Female'}, {name: 'Chris',age: 36,sex: 'Male'}]},components: {'my-component': {template: '#myComponent',props: {data: Array,columns: Array,filterKey: String}}}})</script> </html>search效果:
除了以上介紹的知識點,這個示例還用到了兩個知識點:
5、 prop驗證
props: {data: Array,columns: Array,filterKey: String }這段代碼表示:父組件傳遞過來的data和columns必須是Array類型,filterKey必須是字符串類型。更多prop驗證的介紹,請參考:官方文檔prop驗證
6、filterBy過濾器
可以根據(jù)指定的字符串過濾數(shù)據(jù)。
注意:只有vue1.0才有這些默認(rèn)的過濾器,vue2.0需要自己自定義過濾器
?
轉(zhuǎn)載于:https://www.cnblogs.com/goloving/p/8630603.html
總結(jié)
以上是生活随笔為你收集整理的Vue组件基础知识总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git的使用学习(三)时光机穿梭
- 下一篇: html5倒计时秒杀怎么做,vue 设