vnode.js
1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 注册函数式组件ref
export function registerRef(vnode, context) {
if (!context.data.ref || !vnode.data.hook) {
return vnode;
}
// 备份vnode原有的insert周期函数
const hackInsert = vnode.data.hook.insert;
// 新的vnode的insert周期函数
vnode.data.hook.insert = function(config) {
hackInsert(config);
// 当vnode生成实例后,通过上下文反写入父组件的refs;
context.parent.$refs[context.data.ref] = config.componentInstance || config.elm; // ref本身就有组件实例和dom节点两种情况,优先取实例
};
return vnode;
}
// 简写注册ref
export function ref(name, context) {
const vnode = context._c(name, renderContext(context));
return registerRef(vnode, context);
}
// 清除attrs上的数组和对象,仅用props传递
function clearAttrs(attrs) {
const newAttrs = Object.assign({}, attrs);
Object.keys(newAttrs).forEach(key => {
if (typeof newAttrs[key] === 'function' || typeof newAttrs[key] === 'object') {
delete newAttrs[key];
}
});
delete newAttrs.slot;
return newAttrs;
}
// 渲染函数式组件context
export function renderContext(context) {
const result = {
staticClass: context.data.staticClass,
class: context.data.class,
staticStyle: context.data.staticStyle,
style: context.data.style,
attrs: clearAttrs(context.data.attrs),
props: context.props,
on: context.listeners,
directives: context.data.directives,
scopedSlots: context.scopedSlots,
slot: context.data.slot,
key: context.data.key,
ref: context.data.ref,
refInFor: true,
};
return result;
}
export default {
registerRef,
ref,
};