vue之mixin理解与使用
使用场景
当有两个非常相似的组件,除了一些个别的异步请求外其余的配置都一样,甚至父组件传的值也是一样的,但他们之间又存在着足够的差异性,这时候就不得不拆分成两个组件,如果拆分成两个组件,你就不得不冒着一旦功能变动就要在两个文件中更新代码的风险。
这时候就可以使用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理解与使用的更多相关文章
- Vue中mixin的用法
在项目中我们经常会遇到多个组件调用同一个方法的问题,为了避免每次都在.vue文件中定义并调用,我们可采用vue的mixin的用法: 具体使用如下: 我们需要在main.js中引入mixins文件夹下的 ...
- 沉淀,再出发:VUE的简单理解
沉淀,再出发:VUE的简单理解 一.前言 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过 ...
- vue的mixin简化开发
vue的mixin可以将多个组件公用的声明周期方法和数据封装成一个对象,在不同的组件中自由插拔.实际做项目的时候,可以定义一些mixin,供多个组件使用.也非常有必要定义一个全局的mixin对象,对所 ...
- vue 混入 mixin,自定义指令,过滤器
vue 混入 mixin ,分发 vue 组件中重复的功能 局部的书写格式 // mixin.js var mymixin = { // 这是一个对象:对象里面的写法与组件里面的写法一模一样,组件该 ...
- vue中mixin的理解与用法
vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况下引入组件有什么区别? 组件在引 ...
- vue中mixin的一点理解
vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况下引入组件有什么区别? ...
- vue props的理解
vue用了这么久,今天发现父子组件还是傻傻的分不清,不过还好,今天终于搞懂了 vue中到底什么是父组件,什么是子组件 vue之props父子组件之间的谈话 简单的理解就是:使用的地方是父组件,定义的地 ...
- vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制
一.定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图 ...
- 对vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制
一.定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图 ...
随机推荐
- Apple iPhone 12 Pro 数据迁移方式 All In One
Apple iPhone 12 Pro 数据迁移方式 All In One iPhone 12 Pro https://mp.weixin.qq.com/s/US1Z_69zVQIhV-cNW1E6A ...
- TDD & Unit testing
TDD & Unit testing TDD jest https://github.com/facebook/jest https://facebook.github.io/jest/zh- ...
- App Store Connect
App Store Connect https://developer.apple.com/support/app-store-connect/ https://developer.apple.com ...
- flutter practical
flutter practical https://flutterchina.club/ https://github.com/flutterchina/flutter-in-action https ...
- scrollTo & js auto scroll & scrollX & scrollY
scrollTo & js auto scroll & scrollX & scrollY scrollX & scrollY 获取 scroll top height ...
- React & Desktop App
React & Desktop App https://proton-native.js.org/#/ https://github.com/kusti8/proton-native
- 聚焦 2021 NGK 新加坡区块链技术峰会,探讨DeFi未来新生态!
2021年1月31日14时,备受行业关注的"2021 NGK 新加坡区块链技术峰会"如期举行.本次峰会由NGK官方主办,以"DeFi"为主题,探讨了区块链技术革 ...
- Ubuntu 下安装Anaconda + 显卡驱动 + CUDA + CUDNN + 离线安装环境
写来给自己备忘,并不是什么教程- .- 下载安装包 Anaconda:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 显卡驱动:https ...
- vue-eahars生产编译报错
{ test: /\.js$/, loader: 'babel-loader', include: [resolve('src'), resolve('test'), resolve('node_mo ...
- vue页面嵌套其他页面判断是否生产https
if (location.protocol.indexOf('https') > -1) { var oMeta = document.createElement('meta'); oMeta. ...