@morjs/runtime-web
Version:
mor runtime for web
64 lines • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* 组件支持混入功能
* @param {*} options
*/
function default_1(options) {
const mixins = options.mixins;
if (mixins && mixins instanceof Array) {
mixins.forEach((mixin) => {
if (typeof mixin === 'object') {
for (const key in mixin) {
const value = mixin[key];
switch (key) {
case 'data': {
options.data = Object.assign(Object.assign({}, value), options.data);
break;
}
case 'props': {
options.props = Object.assign(Object.assign({}, value), options.props);
break;
}
case 'methods': {
options.methods = Object.assign(Object.assign({}, value), options.methods);
break;
}
// 生命周期函数
case 'didUnmount':
case 'didUpdate':
case 'deriveDataFromProps':
case 'didMount':
case 'onInit': {
mergeFuction(options, key, value);
break;
}
default: {
options[key] = value;
break;
}
}
}
}
});
delete options.mixins;
}
return options;
}
exports.default = default_1;
function mergeFuction(options, name, func) {
if (!func || typeof func !== 'function')
return;
const orgini = options[name];
if (orgini && typeof orgini === 'function') {
options[name] = function (...args) {
// TODO: need try catch ???
orgini.call(this, ...args);
func.call(this, ...args);
};
}
else {
options[name] = func;
}
}
//# sourceMappingURL=component-mixin.js.map