nimble-ui
Version:
93 lines (89 loc) • 2.51 kB
JavaScript
import { callFn } from 'nimble-lib';
// let _Vue;
export default class MountCom {
constructor(Vue) {
this._Vue = this._Vue || Vue;
}
/**
* 初始化显示的组件
*
* @private
* @template T
* @param {Type<T>} Component 组件
* @param {IPopupComOptions} [options] 组件选项
* @returns {T}
* @memberof SDialogService
*/
_initCom(Component, options) {
let _that = this;
let Com = Component;
options = options || {};
let _parent = options.parent;
if (!_parent) { // 获取父组件
let $parent = callFn(_that._getParent, [], _that);
if ($parent && $parent.$vnode) {
_parent = $parent;
}
}
let params;
if (_parent) {
params = {
parent: _parent,
store: _parent.$store || (_parent.$parent && _parent.$parent.$store)
};
}
if (!(Component instanceof Function)) {
if (!(_that._Vue instanceof Function)) {
console.log(new Error('挂载组件时候未找到Vue'));
return {};
}
Com = _that._Vue.extend(Component);
}
if (options && options.props) {
options.propsData = options.props;
delete options.props;
}
let _com = new Com(Object.assign({}, options, params || {}));
return _com;
}
/**
* 插入组件到页面
*
* @template T
* @param {Type<T>} component 组件
* @param {IPopupComOptions} [options] 选项参数
* @returns {VueComponent}
* @memberof SPopupService
*/
instCom(component, options) {
let _that = this;
options = options || {};
let _el = options.el;
if (!_el) {
_el = document.body;
}
options.el = document.createElement('div');
callFn(_el.appendChild, [options.el], _el);
let _com = _that._initCom(component, options);
return _com;
}
/**
* 更新data或props数据
*
* @param {Object} data 组件的data或props属性
* @param {Object} nowData 更新的数据
* @memberof SPopupService
*/
updataProps(data, nowData) {
if (data && nowData) {
Object.assign(data, nowData);
}
}
/**
* 设置vue
* @param {*} vue vue
*/
setVue(vue) {
this._Vue = vue || this._Vue;
}
}