参考链接https://www.cnblogs.com/luguankun/p/10849636.html
https://www.jianshu.com/p/19705ef09354
mixins就是定义一部分公共的方法或者计算属性,然后混入到各个组件中使用,方便管理与统一修改
什么时候使用Mixins?
页面的风格不用,但是执行的方法和需要的数据类似
同一个生命周期,mixins对象会比组件的先执行。
创建一个js文件作为mixins
在同一个mixins文件中,可以存在多个mixins对象
export const mixinsTest = { methods:{ hello(){ console.log("hello"); } }, created(){ this.hello() } }
export const mixinsTest2 = { methods:{ hello2(){ console.log("hello2"); } }, created() { this.hello2(); }, }
在其他组件中引入这个mixins
import {mixinsTest,mixinsTest2} from '../util/test.js'
引用和使用
<template> <div> home </div> </template> <script> import {mixinsTest,mixinsTest2} from '../util/test.js' export default { name: "Home", data () { return { }; }, created(){ console.log("1212"); }, mixins:[mixinsTest2,mixinsTest] // 先调用那个mixins对象,就先执行哪个 } </script> <style lang="css" scoped> </style>