參考鏈接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>