使用场景

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

  这时候就可以使用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混入你所需要的相同配置或者方法,这样会使我们的开发效率大大提高。

vue之mixin理解与使用的更多相关文章

  1. Vue中mixin的用法

    在项目中我们经常会遇到多个组件调用同一个方法的问题,为了避免每次都在.vue文件中定义并调用,我们可采用vue的mixin的用法: 具体使用如下: 我们需要在main.js中引入mixins文件夹下的 ...

  2. 沉淀,再出发:VUE的简单理解

    沉淀,再出发:VUE的简单理解 一.前言 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过 ...

  3. vue的mixin简化开发

    vue的mixin可以将多个组件公用的声明周期方法和数据封装成一个对象,在不同的组件中自由插拔.实际做项目的时候,可以定义一些mixin,供多个组件使用.也非常有必要定义一个全局的mixin对象,对所 ...

  4. vue 混入 mixin,自定义指令,过滤器

    vue 混入 mixin ,分发 vue 组件中重复的功能 局部的书写格式 // mixin.js var mymixin = {  // 这是一个对象:对象里面的写法与组件里面的写法一模一样,组件该 ...

  5. vue中mixin的理解与用法

    vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况下引入组件有什么区别? 组件在引 ...

  6. vue中mixin的一点理解

    vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况下引入组件有什么区别?     ...

  7. vue props的理解

    vue用了这么久,今天发现父子组件还是傻傻的分不清,不过还好,今天终于搞懂了 vue中到底什么是父组件,什么是子组件 vue之props父子组件之间的谈话 简单的理解就是:使用的地方是父组件,定义的地 ...

  8. vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制

    一.定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图 ...

  9. 对vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制

    一.定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图 ...

随机推荐

  1. js 快速排序 All In One

    js 快速排序 All In One 快速排序 / Quick Sort "use strict"; /** * * @author xgqfrms * @license MIT ...

  2. Web 实时通信方案 All In One

    Web 实时通信方案 All In One HTTP 轮询, 单向通信,开销大 HTTP 长轮询, 单向通信,开销较小 WebSocket,双向通信,开销小 (TCP 高延迟,保证数据完整性) Ser ...

  3. GreenSock & SVG Animation

    GreenSock & SVG Animation refs https://greensock.com/ https://greensock.com/learning/ GSAP https ...

  4. perl 打印目录结构

    更多 #!/usr/bin/perl # 递归打印目录结构 use v5.26; use strict; use utf8; use autodie; use warnings; use Encode ...

  5. Flutter: random color

    import 'dart:math' as math; import 'package:flutter/material.dart'; void main() => runApp(App()); ...

  6. NGK公链如何构建区块链数字经济商业帝国?

    2020年对于区块链市场来说,重大的利好消息莫过于NGK公链的上线了.NGK公链其广泛的市场前景.顶尖的技术,一直备受众多大型机构以及投资者所看好.同时,NGK公链也不负众望,在上线以后,就开始落地到 ...

  7. Docker使用指南

    上文简单介绍了docker,这边记录一下docker的使用. 一.Docker启停 1.启动docker systemctl start docker 2.关闭docker systemctl sto ...

  8. spring-ioc心得

    1.创建spring容器,严格的来说就是创建ClassPathXmlApplicationContext对象, 该对象属于ApplicationContext类型(是一个接口)该接口下有很多实现类, ...

  9. 🤔 移动端 JS 引擎哪家强?美国硅谷找......

    如果你喜欢我写的文章,可以把我的公众号设为星标 ,这样每次有更新就可以及时推送给你啦 在一般的移动端开发场景中,每次更新应用功能都是通过 Native 语言开发并通过应用市场版本分发来实现的.但是市场 ...

  10. # PyComCAD介绍及开发方法

    项目地址:https://github.com/JohnYang1210/PycomCAD 1.综述 ​ 提到Autocad在工业界的二次开发,VB或者Lisp可能作为常用的传统的编程语言.但是,Py ...