Skip to content

Commit

Permalink
feat: add setupContext
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jan 29, 2021
1 parent dc8e4b6 commit 4574107
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 4 additions & 1 deletion example/components/renderComponent.js/Child.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { h, ref, reactive } from "../../../lib/mini-vue.esm.js";
export default {
name: "Child",
setup() {},
setup(props, context) {
console.log("props------------------>", props);
console.log("context---------------->", context);
},
render() {
return h("div", {}, "child");
},
Expand Down
17 changes: 11 additions & 6 deletions src/runtime-core/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export function createComponentInstance(vnode) {
props: {},
proxy: null,
isMounted: false,
attrs: {}, // 存放 attrs 的数据
slots: {}, // 存放插槽的数据
emit: () => {}, // TODO 需要实现 emit 函数
};

return instance;
Expand Down Expand Up @@ -44,20 +47,22 @@ function setupStatefulComponent(instance) {
// 调用 setup 的时候传入 props
const { setup } = Component;
if (setup) {
const setupContext = createSetupContext();
const setupContext = createSetupContext(instance);
const setupResult = setup && setup(instance.props, setupContext);
}

// 3. 处理 setupResult
handleSetupResult(instance, setupResult);
}

function createSetupContext() {
// TODO
// 需要实现 emit
// slots
function createSetupContext(instance) {
console.log("初始化 setup context");
return {};
return {
attrs: instance.attrs,
slots: instance.slots,
emit: instance.emit,
expose: () => {}, // TODO 实现 expose 函数逻辑
};
}

function handleSetupResult(instance, setupResult) {
Expand Down

0 comments on commit 4574107

Please sign in to comment.