Skip to content

Commit 12cf2f5

Browse files
author
lishiwen
committed
added positoin renderslots
1 parent b59eae5 commit 12cf2f5

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

example/slots-simple/App.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ export default {
1111
SlotInstance,
1212
{},
1313
// 在Slot Children设置Slot Component, 期望可以在内部显示出来
14-
h("span", {}, "这是插入的内容")
14+
{
15+
header: h("span", {}, "Header"),
16+
footer: h("span", {}, "Footer"),
17+
}
1518
);
1619
return h("div", {}, [Bar, Slot]);
1720
},
21+
1822
setup() {},
1923
};

example/slots-simple/Slot.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { h, renderSlots } from "../../lib/m-vue.esm.js";
33
export default {
44
render() {
55
const originalContent = h("div", {}, "这是原来的内容");
6-
return h("div", {}, [originalContent, renderSlots(this.$slots)]);
6+
return h("div", {}, [
7+
renderSlots(this.$slots, "header"),
8+
originalContent,
9+
renderSlots(this.$slots, "footer"),
10+
]);
711
},
812
setup() {},
913
};

lib/m-vue.cjs.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ const ComponentPublicInstanceHandlers = {
191191
};
192192

193193
function initSlots(instance, instanceChildren) {
194-
instance.slots = isArray(instanceChildren) ? instanceChildren : [instanceChildren];
194+
let slots = {};
195+
if (isStructObject(instanceChildren)) {
196+
for (const key in instanceChildren) {
197+
const slot = instanceChildren[key];
198+
slots[key] = isArray(slot) ? slot : [slot];
199+
}
200+
}
201+
instance.slots = slots;
195202
}
196203

197204
function promiseEmit() { }
@@ -329,8 +336,10 @@ function h(type, props, children) {
329336
return createVNode(type, props, children);
330337
}
331338

332-
function renderSlots(slots) {
333-
return createVNode('span', null, slots);
339+
function renderSlots(slots, renderName) {
340+
if (hasOwnProperty(slots, renderName)) {
341+
return createVNode('span', null, slots[renderName]);
342+
}
334343
}
335344

336345
exports.createApp = createApp;

lib/m-vue.esm.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ const ComponentPublicInstanceHandlers = {
187187
};
188188

189189
function initSlots(instance, instanceChildren) {
190-
instance.slots = isArray(instanceChildren) ? instanceChildren : [instanceChildren];
190+
let slots = {};
191+
if (isStructObject(instanceChildren)) {
192+
for (const key in instanceChildren) {
193+
const slot = instanceChildren[key];
194+
slots[key] = isArray(slot) ? slot : [slot];
195+
}
196+
}
197+
instance.slots = slots;
191198
}
192199

193200
function promiseEmit() { }
@@ -325,8 +332,10 @@ function h(type, props, children) {
325332
return createVNode(type, props, children);
326333
}
327334

328-
function renderSlots(slots) {
329-
return createVNode('span', null, slots);
335+
function renderSlots(slots, renderName) {
336+
if (hasOwnProperty(slots, renderName)) {
337+
return createVNode('span', null, slots[renderName]);
338+
}
330339
}
331340

332341
export { createApp, createComponentInstance, createVNode, h, patch, render, renderSlots, setupComponent };

src/runtime-core/componentSlots.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { isArray } from "../shared/type";
1+
import { isArray, isStructObject } from "../shared/type";
22

33
export function initSlots(instance, instanceChildren) {
4-
instance.slots = isArray(instanceChildren) ? instanceChildren : [instanceChildren];
4+
let slots = {};
5+
if (isStructObject(instanceChildren)) {
6+
for (const key in instanceChildren) {
7+
const slot = instanceChildren[key];
8+
slots[key] = isArray(slot) ? slot : [slot];
9+
}
10+
};
11+
instance.slots = slots;
512
};
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { hasOwnProperty } from "../../shared/index";
12
import { createVNode } from "../createVNode";
23

3-
export function renderSlots(slots) {
4-
return createVNode('span', null, slots);
4+
export function renderSlots(slots, renderName) {
5+
if (hasOwnProperty(slots, renderName)) {
6+
return createVNode('span', null, slots[renderName]);
7+
}
58
}

0 commit comments

Comments
 (0)