首页 技术 正文
技术 2022年11月9日
0 收藏 992 点赞 5,012 浏览 3138 个字

使用场景

  当有两个非常相似的组件,除了一些个别的异步请求外其余的配置都一样,甚至父组件传的值也是一样的,但他们之间又存在着足够的差异性,这时候就不得不拆分成两个组件,如果拆分成两个组件,你就不得不冒着一旦功能变动就要在两个文件中更新代码的风险。

  这时候就可以使用mixin(混入)了,混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的选项。可能听起来比较抽象,现在举个简单的例子吧。

实际案例

  对比这两个组件有什么不同和相同之处

//组件一
<template>
<a-table :columns="columns" :data-source="data" :pagination="pagination" :loading="loading" rowKey="classid">
<a slot="classname" slot-scope="text">{{ text }}</a>
<span slot="crtime" slot-scope="text, record">
{{ parseTimeNew(record.crtime) }}
</span>
</a-table>
</template>
<script>
import { findClassHourByCurricid } from '@/api/system/class'
export default {
name: 'AllClassHour',
props: {
recordDeatil: {
type: Object,
required: true
},
detailShow: {
type: Boolean,
require: true,
default: false
}
},
data () {
return {
data: [],
columns: [
{
title: '序号',
dataIndex: 'index',
key: 'index',
align: 'center',
width: '10%',
customRender: (text, record, index) => `${index + 1}`
},
{
dataIndex: 'classname',
title: '课时名称',
key: 'classname',
width: '50%',
// align: 'center',
scopedSlots: { customRender: 'classname' }
},
{
title: '创建日期',
dataIndex: 'crtime',
key: 'crtime',
width: '50%',
// align: 'center',
scopedSlots: { customRender: 'crtime' }
}
],
pagination: false,
loading: false,
status: false
}
},
mounted () {this.getClassHour()
this.test()
}, methods: {
test () {
console.log('测试公共组件')
},
getClassHour () {
this.data = []
const params = {
curricid: this.recordDeatil.curricid
}
this.loading = true
findClassHourByCurricid(params).then(res => {
const classHourDetail = res.data.data
this.data = classHourDetail
this.loading = false
}
)
}
}
}</script>
//组件二
<template>
<a-table :columns="columns" :data-source="data" :pagination="pagination" :loading="loading" rowKey="userid">
<a slot="truename" slot-scope="text">{{ text }}</a>
<span slot="crtime" slot-scope="text, record">
{{ parseTimeNew(record.crtime) }}
</span>
</a-table>
</template>
<script>
import { findStudentByCurricid } from '@/api/system/class'
export default {
name: 'AllStudent',
props: {
recordDeatil: {
type: Object,
required: true
},
detailShow: {
type: Boolean,
require: true,
default: false
}
},
data () {
return {
data: [],
columns: [
{
title: '序号',
dataIndex: 'index',
key: 'index',
align: 'center',
width: '10%',
customRender: (text, record, index) => `${index + 1}`
},
{
dataIndex: 'truename',
title: '真实姓名',
key: 'truename',
width: '50%',
// align: 'center',
scopedSlots: { customRender: 'truename' }
},
{
title: '中文名',
dataIndex: 'chanema',
width: '50%',
// align: 'center',
key: 'chanema'
}
],
pagination: false,
loading: false,
status: false
}
},
mounted () {this.getStudent()
this.test()
}, methods: {
test () {
console.log('测试公共组件')
},
getStudent () {
this.data = []
const params = {
curricid: this.recordDeatil.curricid
}
this.loading = true
findStudentByCurricid(params).then(res => {
const studentDetail = res.data.data
this.data = studentDetail
this.loading = false
}
)
}
}
}</script>

可以发现,除了获取表格的数据所调用的异步请求外其余配置基本上相同 于是我们可以在这里提取逻辑并创建可以被重用的项:

export const publish = {
props: {
recordDeatil: {
type: Object,
required: true
},
detailShow: {
type: Boolean,
require: true,
default: false
}
},
data () {
return {
data: [],
pagination: false,
loading: false,
status: false
}
},
methods: {
test () {
console.log('测试公共方法')
}
}
}

然后我们把组件中重复的配置和方法全部去掉,引用这个mixin

运行代码会发现 结果是一样的

即便我们使用的是一个对象而不是一个组件,生命周期函数对我们来说仍然是可用的,理解这点很重要。我们也可以这里使用mounted()钩子函数,它将被应用于组件的生命周期上。这种工作方式真的很灵活也很强大。

总而言之

  在一些我们需要做同样配置或者相似度极高的组件时,我们不妨可以试试Mixin混入你所需要的相同配置或者方法,这样会使我们的开发效率大大提高。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291