小程序代理Page实现VUE中mixins效果
2023-03-31

//mixins.js

function mixinPage () {

const originPage = Page
   Page = (options) => {
options = mergePage(options.mixins, options)
       originPage(options)
   }
}
const ORIGIN_PROPERTIES_PAGE = [
   'data'
]
const ORIGIN_METHODS = [
   'onLoad',
'onReady',
'onShow',
'onHide',
'onUnload',
'onPullDownRefresh',
'onReachBottom',
'onShareAppMessage',
'onPageScroll',
'onResize',
'onTabItemTap'
]
function mergePage (mixins, options) {
mixins.forEach((mixin) => {
if (typeof mixin == 'object') {
for (let key in mixin) {
if (ORIGIN_PROPERTIES_PAGE.includes(key)) {
options[key] = { ...mixin[key], ...options[key] }
} else if (ORIGIN_METHODS.includes(key)) {
const originFunc = options[key]
                   options[key] = function (...args) {
mixin[key].call(this, ...args)
                       return originFunc && originFunc.call(this, ...args)
                   }
} else {
options = { ...mixin, ...options }
}
}
}
})
   return options
}

mixinPage()

module.exports = {

    data: {

        aaa999: ''

    },

    onShow() {

        this.setData({

                'aaa999':333

        })

    }

}


//混入文件

module.exports = {

    data: {

       a:1

    },

    onShow() {

    },

  XXXXX(){

 },

}


//页面配置

Page({

mixins:[......]