vue之mixin理解与使用🍉

简介: vue之mixin理解与使用🍉

🍕 前言


  • 最近确实是有点忙,天天日更确实有点不知道写什么了,所以就把以前自己记录的文章拿出来吧。
  • 可能会有点水,大家如果有需要的可以看看。
  • mixin可以让我们的组件复用一些我们配置相同的生命周期或者方法,当然这个mixin只能在vue 2.x使用,vue 3.0已经不需要了。


🥪 使用场景


  • 当有两个非常相似的组件,除了一些个别的异步请求外其余的配置都一样,甚至父组件传的值也是一样的,但他们之间又存在着足够的差异性,这时候就不得不拆分成两个组件,如果拆分成两个组件,你就不得不冒着一旦功能变动就要在两个文件中更新代码的风险。
  • 这时候就可以使用mixin(混入)了,混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的选项。可能听起来比较抽象,现在举个简单的例子吧。


🥧 实际案例


  • 对比这两个组件的script有什么不同和相同之处
//组件一
<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>
//组件二
<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混入你所需要的相同配置或者方法,这样会使我们的开发效率大大提高。
  • 当然这个mixin只适用于vue 2.x,vue 3.0Composition API已经很强大了。
相关文章
|
4天前
|
资源调度 JavaScript API
Vue-treeselect:为Vue应用程序提供强大选择器的指南
Vue-treeselect:为Vue应用程序提供强大选择器的指南
13 0
|
4天前
|
JavaScript 前端开发
Vue,如何引入样式文件
Vue,如何引入样式文件
|
4天前
|
JavaScript
|
3天前
|
JavaScript 开发工具 git
大事件项目40---Vue代码里如何引入相对路径图片
大事件项目40---Vue代码里如何引入相对路径图片
|
4天前
|
JavaScript
vue滚动到页面底部时加载
vue滚动到页面底部时加载
6 1
|
1天前
|
JavaScript 程序员
程序员必知:Vue子传父的三种方法
程序员必知:Vue子传父的三种方法
|
1天前
|
JavaScript 开发者
|
1天前
|
JavaScript
vue : 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
vue : 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
|
3天前
|
JavaScript 前端开发
一个好看的vue admin模板
这是一个关于Vue管理模板的引用,提到了[PanJiaChen](https://github.com/PanJiaChen/vue-admin-template)在GitHub上的`vue-admin-template`项目。该项目是一个前端管理模板,链接指向了详细的资源。页面中还包含了一张图片,但markdown格式中无法直接显示。简而言之,这是关于一个基于Vue的后台管理界面模板的参考信息。