這篇文章主要介紹了淺談Vue內置component組件的應用場景,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
官方的說明
渲染一個“元組件”爲動態組件。依 is 的值,來決定哪個組件被渲染。
1
2
|
<!-- 動態組件由 vm 實例的屬性值 `componentId` 控制 -->
< component :is = "componentId" ></ component >
|
具體可以官網文檔中的
動態組件
內置的組件component
場景
這裏通過一個業務場景來闡述vue內置component組件的應用。 如圖所示,這裏展示經典註冊頁面,註冊分爲郵箱註冊和手機註冊,彈窗頂部有標籤可以切換註冊類型,中間是註冊表單信息,郵箱註冊和手機註冊有着不一樣的表單內容,底部是註冊按鈕以及其他操作。 經過分析手機註冊界面與郵箱註冊除了中間的表單內容不一致之外,其他的界面內容是一樣的。
正在上傳…重新上傳取消
實際項目代碼設計中,爲了保證複用性和可維護性,是會有一些可行的方案。這裏我們採用vue內置的component組件來實現這一點。
核心代碼實現
頂部tab切換的時候,type值發生改變,對應的表單的組件也發生了變化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<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內置component組件情況下,一般實際被渲染的組件具有一定的共性,比如相同的屬性,相同的方法或者相同的初始化銷燬過程。比如目前這個場景中郵箱表單和手機表單都具有校驗方法(validateData)和獲取表單數據方法(getFormData)。 這種情況下可以使用vue提供的混合的功能。進一步抽離 mixins.js
1
2
3
4
5
6
7
8
9
10
|
export default {
methods: {
validateData() {
return Promise.resolve()
},
getFormData() {
return {}
}
}
}
|
email-form.vue
1
2
3
4
5
6
7
8
9
10
11
|
<script>
import minx from './mixins'
export default {
mixins: [mixins],
methods: {
getFormData() {
return { email: '[email protected]' }
}
}
}
</script>
|
如果有自定義的需求,可以重寫mixins中的方法。
表格的應用
在管理後臺項目中,表格經常會被用到。我們希望表格的td是文本、進度條、checkbox等等,且希望通過傳一個json配置就可以渲染出。使用vue內置的component組件可以起到很讚的作用。
轉存失敗重新上傳取消
比如這樣的一個table使用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<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的實現
1
2
3
|
< td v-for = "column of columns" >
< component :is = "`${TYPE_PRE}${columns.render.type}`" :row-data = "rowData" :params = "columns.render" ></ component >
</ td >
|
表單的應用
在管理後臺項目中,表單也經常需要用到,我們也同樣希望表單的某一項是文本框,下拉框,時間選擇框,富文本等等等等,且希望通過傳一個json配置就可以渲染出。vue內置的component組件可以依然可以實現這樣一個美好的願景。
轉存失敗重新上傳取消
比如這樣的一個form使用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<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: '啓用標誌' ,
type: 'dropdown' ,
extra: {options: [{ label: '啓用' , value: 1 }, { label: '禁用' , value: 2 }] }
}
]
}
}
}
</script>
|
form中使用component的實現
1
2
3
4
5
6
7
8
9
10
11
12
|
< 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的後臺引擎 開源項目中都有實現,歡迎star和fork。