Vue中如何使用component組件,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

內(nèi)置的組件component
場景
這里通過一個業(yè)務(wù)場景來闡述vue內(nèi)置component組件的應(yīng)用。 如圖所示,這里展示經(jīng)典注冊頁面,注冊分為郵箱注冊和手機注冊,彈窗頂部有標(biāo)簽可以切換注冊類型,中間是注冊表單信息,郵箱注冊和手機注冊有著不一樣的表單內(nèi)容,底部是注冊按鈕以及其他操作。 經(jīng)過分析手機注冊界面與郵箱注冊除了中間的表單內(nèi)容不一致之外,其他的界面內(nèi)容是一樣的。

實際項目代碼設(shè)計中,為了保證復(fù)用性和可維護性,是會有一些可行的方案。這里我們采用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>
<h4>新用戶注冊</h4>
<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提供的混合的功能。進一步抽離 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是文本、進度條、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: '進度組件', dataKey: 'progress', render: { type: 'progress2', max: 100, precision: 2 } }
],
datum: [{ id: '1', name: '進度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>看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章標(biāo)題:Vue中如何使用component組件-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://www.chinadenli.net/article4/pcjie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、網(wǎng)站收錄、網(wǎng)站策劃、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容