Vue組件基礎(chǔ)用法詳解
Vue組件概述
組件(Component)是Vue.js最強(qiáng)大的功能之一。組件可以擴(kuò)展HTML元素,封裝可重用的代碼。根據(jù)項(xiàng)目需求,抽象出一些組件,每個(gè)組件里包含了展現(xiàn)、功能和樣式。每個(gè)頁(yè)面,根據(jù)自己所需,使用不同的組件來(lái)拼接頁(yè)面。這種開(kāi)發(fā)模式使前端頁(yè)面易于擴(kuò)展,且靈活性高,而且組件之間也實(shí)現(xiàn)了解耦。
在 Vue 里,一個(gè)組件本質(zhì)上是一個(gè)擁有預(yù)定義選項(xiàng)的一個(gè) Vue 實(shí)例
組件是一個(gè)自定義元素或稱為一個(gè)模塊,包括所需的模板、邏輯和樣式。在HTML模板中,組件以一個(gè)自定義標(biāo)簽的形式存在,起到占位符的功能。通過(guò)Vue.js的聲明式渲染后,占位符將會(huì)被替換為實(shí)際的內(nèi)容
下面是一個(gè)最簡(jiǎn)單的模塊示例
<div id="app"> <xiaohuochai></xiaohuochai> </div>
Vue注冊(cè)組件
組件注冊(cè)包括全局注冊(cè)和局部注冊(cè)兩種
全局注冊(cè)
要注冊(cè)一個(gè)全局組件,可以使用 Vue.component(tagName, options)
Vue.component('my-component', {
// 選項(xiàng)
})
組件在注冊(cè)之后,便可以在父實(shí)例的模塊中以自定義元素 <my-component></my-component> 的形式使用
[注意]要確保在初始化根實(shí)例之前注冊(cè)了組件
<div id="example">
<my-component></my-component>
</div>
<script>
// 注冊(cè)
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
局部注冊(cè)
通過(guò)使用組件實(shí)例選項(xiàng)components注冊(cè),可以使組件僅在另一個(gè)實(shí)例/組件的作用域中可用
<div id="example">
<my-component></my-component>
</div>
<script>
// 注冊(cè)
var Child = {
template: '<div>A custom component!</div>'
};
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example',
components: {
// <my-component> 將只在父模板可用
'my-component': Child
}
})
</script>
組件樹(shù)
使用組件實(shí)例選項(xiàng)components注冊(cè),可以實(shí)現(xiàn)組件樹(shù)的效果
<div id="example">
<my-component></my-component>
</div>
<script>
// 注冊(cè)
var headerTitle = {
template: '<p>我是標(biāo)題</p>',
};
var headerContent = {
template: '<p>我是內(nèi)容</p>',
};
var header = {
template: `
<div class="hd">
<header-content></header-content>
<header-title></header-title>
</div>
`,
components: {
'header-content': headerContent,
'header-title': headerTitle
}
};
// 創(chuàng)建實(shí)例
new Vue({
el: '#example',
components: {
'my-component': header
}
})
</script>
對(duì)于大型應(yīng)用來(lái)說(shuō),有必要將整個(gè)應(yīng)用程序劃分為組件,以使開(kāi)發(fā)可管理。一般地組件應(yīng)用模板如下所示
<div id="app"> <app-nav></app-nav> <app-view> <app-sidebar></app-sidebar> <app-content></app-content> </app-view> </div>
v-once
盡管在 Vue 中渲染 HTML 很快,不過(guò)當(dāng)組件中包含大量靜態(tài)內(nèi)容時(shí),可以考慮使用 v-once 將渲染結(jié)果緩存起來(lái)
Vue.component('my-component', {
template: '<div v-once>hello world!...</div>'
})
Vue組件的模板分離
在組件注冊(cè)中,使用template選項(xiàng)中拼接HTML元素比較麻煩,這也導(dǎo)致了HTML和JS的高耦合性。慶幸的是,Vue.js提供了兩種方式將定義在JS中的HTML模板分離出來(lái)
script
在script標(biāo)簽里使用 text/x-template 類型,并且指定一個(gè) id
<script type="text/x-template" id="hello-world-template">
<p>Hello hello hello</p>
</script>
Vue.component('hello-world', {
template: '#hello-world-template'
})
上面的代碼等價(jià)于
Vue.component('hello-world', {
template: '<p>Hello hello hello</p>'
})
下面是一個(gè)簡(jiǎn)單示例
<div id="example">
<my-component></my-component>
</div>
<script type="text/x-template" id="hello-world-template">
<div>hello world!</div>
</script>
<script>
Vue.component('my-component', {
template: '#hello-world-template'
})
new Vue({
el: '#example'
})
</script>
template
如果使用<template>標(biāo)簽,則不需要指定type屬性
<div id="example">
<my-component></my-component>
</div>
<template id="hello-world-template">
<div>hello world!</div>
</template>
<script>
// 注冊(cè)
Vue.component('my-component', {
template: '#hello-world-template'
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
Vue組件的命名約定
對(duì)于組件的命名,W3C規(guī)范是字母小寫(xiě)且包含一個(gè)中劃線(-),雖然Vue沒(méi)有強(qiáng)制要求,但最好遵循規(guī)范
<!-- 在HTML模版中始終使用 kebab-case --> <kebab-cased-component></kebab-cased-component> <camel-cased-component></camel-cased-component> <pascal-cased-component></pascal-cased-component>
當(dāng)注冊(cè)組件時(shí),使用中劃線、小駝峰、大駝峰這三種任意一種都可以
// 在組件定義中
components: {
// 使用 中劃線 形式注冊(cè)
'kebab-cased-component': { /* ... */ },
// 使用 小駝峰 形式注冊(cè)
'camelCasedComponent': { /* ... */ },
// 使用 大駝峰 形式注冊(cè)
'PascalCasedComponent': { /* ... */ }
}
Vue組件嵌套限制
并不是所有的元素都可以嵌套模板,因?yàn)橐艿紿TML元素嵌套規(guī)則的限制,尤其像<ul>,<ol>,<table>,<select> 限制了能被它包裹的元素,而一些像 <option> 這樣的元素只能出現(xiàn)在某些其它元素內(nèi)部
[注意]關(guān)于HTML標(biāo)簽的詳細(xì)嵌套規(guī)則移步至此
在自定義組件中使用這些受限制的元素時(shí)會(huì)導(dǎo)致一些問(wèn)題,例如
<table id="example"> <my-row>...</my-row> </table>
自定義組件 <my-row> 被認(rèn)為是無(wú)效的內(nèi)容,因此在渲染的時(shí)候會(huì)導(dǎo)致錯(cuò)誤
<script>
// 注冊(cè)
var header = {
template: '<div class="hd">我是標(biāo)題</div>'
};
// 創(chuàng)建實(shí)例
new Vue({
el: '#example',
components: {
'my-row': header
}
})
</script>
is屬性
變通的方案是使用特殊的 is 屬性
<table id="example">
<tr is="my-row"></tr>
</table>
<script>
// 注冊(cè)
var header = {
template: '<div class="hd">我是標(biāo)題</div>'
};
// 創(chuàng)建實(shí)例
new Vue({
el: '#example',
components: {
'my-row': header
}
})
</script>
Vue組件的根元素
Vue強(qiáng)制要求每一個(gè)Vue實(shí)例(組件本質(zhì)上就是一個(gè)Vue實(shí)例)需要有一個(gè)根元素
如下所示,則會(huì)報(bào)錯(cuò)
<div id="example">
<my-component></my-component>
</div>
<script>
// 注冊(cè)
Vue.component('my-component', {
template: `
<p>第一段</p>
<p>第二段</p>
`,
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
需要改寫(xiě)成如下所示
<script>
// 注冊(cè)
Vue.component('my-component', {
template: `
<div>
<p>第一段</p>
<p>第二段</p>
</div>
`,
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
Vue組件數(shù)據(jù)傳遞
一般地,我們?cè)赩ue實(shí)例對(duì)象或Vue組件對(duì)象中,我們通過(guò)data來(lái)傳遞數(shù)據(jù)
<div id="example">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
</div>
<script>
// 注冊(cè)
Vue.component('my-component', {
template: '<div>{{message}}</div>',
data:{
message: 'hello'
}
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
運(yùn)行上面的代碼,會(huì)使Vue停止執(zhí)行,并在控制臺(tái)發(fā)出錯(cuò)誤提示,告訴你在組件中 data 必須是一個(gè)函數(shù)
可以用如下方式來(lái)繞開(kāi)Vue的錯(cuò)誤提示
<script>
// 注冊(cè)
var data = {counter: 0}
Vue.component('my-component', {
template: '<button v-on:click="counter += 1">{{ counter }}</button>',
data:function(){
return data;
}
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
由于這三個(gè)組件共享了同一個(gè) data,因此增加一個(gè) counter 會(huì)影響所有組件
當(dāng)一個(gè)組件被定義, data 需要聲明為返回一個(gè)初始數(shù)據(jù)對(duì)象的函數(shù),因?yàn)榻M件可能被用來(lái)創(chuàng)建多個(gè)實(shí)例。如果 data 仍然是一個(gè)純粹的對(duì)象,則所有的實(shí)例將共享引用同一個(gè)數(shù)據(jù)對(duì)象。通過(guò)提供 data 函數(shù),每次創(chuàng)建一個(gè)新實(shí)例后,能夠調(diào)用 data 函數(shù),從而返回初始數(shù)據(jù)的一個(gè)全新副本數(shù)據(jù)對(duì)象
因此,可以通過(guò)為每個(gè)組件返回全新的 data 對(duì)象來(lái)解決這個(gè)問(wèn)題:
<script>
// 注冊(cè)
Vue.component('my-component', {
template: '<button v-on:click="counter += 1">{{ counter }}</button>',
data:function(){
return {counter: 0};
}
})
// 創(chuàng)建根實(shí)例
new Vue({
el: '#example'
})
</script>
現(xiàn)在每個(gè) counter 都有它自己內(nèi)部的狀態(tài)了
Vue組件原生事件
有時(shí)候,可能想在某個(gè)組件的根元素上監(jiān)聽(tīng)一個(gè)原生事件。直接使用v-bind指令是不生效的
<div id="example">
<my-component @click="doTheThing"></my-component>
<p>{{message}}</p>
</div>
<script>
Vue.component('my-component', {
template: '<button>按鈕</button>',
})
new Vue({
el: '#example',
data:{
message:0
},
methods:{
doTheThing(){
this.message++;
}
}
})
</script>
可以使用 .native 修飾 v-on指令即可
<div id="example">
<my-component @click.native="doTheThing"></my-component>
<p>{{message}}</p>
</div>
<script>
Vue.component('my-component', {
template: '<button>按鈕</button>',
})
new Vue({
el: '#example',
data:{
message:0
},
methods:{
doTheThing(){
this.message++;
}
}
})
更多關(guān)于Vue組件的使用方法請(qǐng)點(diǎn)擊下面的相關(guān)鏈接
相關(guān)文章
使用Vue如何寫(xiě)一個(gè)雙向數(shù)據(jù)綁定(面試常見(jiàn))
這篇文章主要介紹了使用Vue如何寫(xiě)一個(gè)雙向數(shù)據(jù)綁定,在前端面試過(guò)程中經(jīng)常會(huì)問(wèn)到,文中主要實(shí)現(xiàn)v-model,v-bind 和v-click三個(gè)命令,其他命令也可以自行補(bǔ)充。需要的朋友可以參考下2018-04-04
vue-cli2.x項(xiàng)目?jī)?yōu)化之引入本地靜態(tài)庫(kù)文件的方法
這篇文章主要介紹了vue-cli2.x項(xiàng)目?jī)?yōu)化之引入本地靜態(tài)庫(kù)文件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)的實(shí)例詳解(params/query)
在使用`router.push`進(jìn)行路由跳轉(zhuǎn)到另一個(gè)組件時(shí),可以通過(guò)`params`或`query`來(lái)傳遞參數(shù),這篇文章主要介紹了vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)(params/query),需要的朋友可以參考下2023-09-09
vue使用elementui的el-menu的折疊菜單collapse示例詳解
這篇文章主要介紹了vue使用elementui的el-menu的折疊菜單collapse示例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
Vue實(shí)現(xiàn)關(guān)聯(lián)頁(yè)面多級(jí)跳轉(zhuǎn)(頁(yè)面下鉆)功能的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)關(guān)聯(lián)頁(yè)面多級(jí)跳轉(zhuǎn)(頁(yè)面下鉆)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
vue踩坑記錄之?dāng)?shù)組定義和賦值問(wèn)題
這篇文章主要給大家介紹了關(guān)于vue踩坑記錄之?dāng)?shù)組定義和賦值問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Vue 3 中的 toRef 和 toRefs 函數(shù)案例講解
這篇文章主要介紹了Vue 3 中的 toRef 和 toRefs 函數(shù),toRef 和 toRefs 函數(shù)是 Vue 3 中的兩個(gè)非常有用的函數(shù),它們可以幫助我們更好地管理組件中的響應(yīng)式數(shù)據(jù),并且可以提高組件的性能和用戶體驗(yàn),需要的朋友可以參考下2024-06-06

