淺談Vue內(nèi)置component組件的應(yīng)用場景
官方的說明
渲染一個“元組件”為動態(tài)組件。依 is 的值,來決定哪個組件被渲染。
<!-- 動態(tài)組件由 vm 實例的屬性值 `componentId` 控制 --> <component :is="componentId"></component>
具體可以官網(wǎng)文檔中的
場景
這里通過一個業(yè)務(wù)場景來闡述vue內(nèi)置component組件的應(yīng)用。 如圖所示,這里展示經(jīng)典注冊頁面,注冊分為郵箱注冊和手機注冊,彈窗頂部有標(biāo)簽可以切換注冊類型,中間是注冊表單信息,郵箱注冊和手機注冊有著不一樣的表單內(nèi)容,底部是注冊按鈕以及其他操作。 經(jīng)過分析手機注冊界面與郵箱注冊除了中間的表單內(nèi)容不一致之外,其他的界面內(nèi)容是一樣的。

實際項目代碼設(shè)計中,為了保證復(fù)用性和可維護(hù)性,是會有一些可行的方案。這里我們采用vue內(nèi)置的component組件來實現(xiàn)這一點。
核心代碼實現(xiàn)
頂部tab切換的時候,type值發(fā)生改變,對應(yīng)的表單的組件也發(fā)生了變化
<template>
<div>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" @click.prevent="handleCloseBtnClick"></a>
<div>
<h3>新用戶注冊</h3>
<div>
<span :class="{active: type === 'mobileForm'}" @click="type = mobileForm">手機注冊</span>
<span :class="{active: type === 'emailForm'}" @click="type = emailForm">郵箱注冊</span>
</div>
</div>
<component :is="type" ref="form">
<button @click="handleRegisterBtnClick">注冊</button>
<div ><span ><span>注冊視為同意</span><a> 《法律條款和隱私說明》</a></span></div>
<div><span>已有賬號<a href="javascript:;" rel="external nofollow" rel="external nofollow" @click.prevent="handleLoginBtnClick">直接登入>></a></span></div>
</component>
</div>
</template>
<script>
export default {
methods: {
handleRegisterBtnClick () {
this.$refs.form.validateData().then(() => {
this.$refs.form.getFormData()
})
}
}
}
</script>
mixins混合
用Vue內(nèi)置component組件情況下,一般實際被渲染的組件具有一定的共性,比如相同的屬性,相同的方法或者相同的初始化銷毀過程。比如目前這個場景中郵箱表單和手機表單都具有校驗方法(validateData)和獲取表單數(shù)據(jù)方法(getFormData)。 這種情況下可以使用vue提供的混合的功能。進(jìn)一步抽離 mixins.js
export default {
methods: {
validateData() {
return Promise.resolve()
},
getFormData() {
return {}
}
}
}
email-form.vue
<script>
import minx from './mixins'
export default {
mixins: [mixins],
methods: {
getFormData() {
return { email: 'example@example.com' }
}
}
}
</script>
如果有自定義的需求,可以重寫mixins中的方法。
表格的應(yīng)用
在管理后臺項目中,表格經(jīng)常會被用到。我們希望表格的td是文本、進(jìn)度條、checkbox等等,且希望通過傳一個json配置就可以渲染出。使用vue內(nèi)置的component組件可以起到很贊的作用。
比如這樣的一個table使用方式
<template>
<vue-table ref="table" :columns="columns" :datum="datum"></vue-table>
</template>
<script>
export default {
data () {
return {
columns: [
{ title: 'ID', width: '30', dataKey: 'id' },
{ title: '進(jìn)度組件', dataKey: 'progress', render: { type: 'progress2', max: 100, precision: 2 } }
],
datum: [{ id: '1', name: '進(jìn)度0', progress: 10 }]
}
}
}
</script>
table中使用component的實現(xiàn)
<td v-for="column of columns">
<component :is="`${TYPE_PRE}${columns.render.type}`" :row-data="rowData" :params="columns.render"></component>
</td>
表單的應(yīng)用
在管理后臺項目中,表單也經(jīng)常需要用到,我們也同樣希望表單的某一項是文本框,下拉框,時間選擇框,富文本等等等等,且希望通過傳一個json配置就可以渲染出。vue內(nèi)置的component組件可以依然可以實現(xiàn)這樣一個美好的愿景。
比如這樣的一個form使用方式
<template>
<c-form :cells="cells" ref="form">
<button class="button is-primary" :class="{ 'is-disabled': isSubmitBtnDisabled }" @click.prevent="submit">提交</button>
</c-form>
</template>
<script>
export default {
computed: {
cells () {
return [
{
field: 'name',
label: '名稱',
type: 'textfield',
attrs: { placeholder: '名稱' },
validate: { required: { message: '請輸入名稱'} }
},
{
field: 'enable',
label: '啟用標(biāo)志',
type: 'dropdown',
extra: {options: [{ label: '啟用', value: 1 }, { label: '禁用', value: 2 }] }
}
]
}
}
}
</script>
form中使用component的實現(xiàn)
<form>
<c-form-cell v-for="cell of cellList" :key="cell.field" :field="cell.field">
<component
:is="`${TYPE_PRE}${cell.type}`"
:field="cell.field"
:attrs="cell.attrs"
:extra="cell.extra"
:validate="cell.validate"
:cells="cell.cells">
</component>
</c-form-cell>
</form>
表單和表格在 基于VUE的后臺引擎 開源項目中都有實現(xiàn),歡迎star和fork。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element tree樹形組件回顯數(shù)據(jù)問題解決
本文主要介紹了element tree樹形組件回顯數(shù)據(jù)問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

