Ai
1 Star 2 Fork 1

旻天信息/mttk-vue-wrap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
compBase.ts 3.86 KB
一键复制 编辑 原始数据 按行查看 历史
旻天信息 提交于 2025-02-07 11:01 +08:00 . v0.3.1
import {
computed,
unref,
toRaw,
vShow,
withDirectives,
h,
SetupContext,
} from "vue";
import { standardizedConfig, getRawValue } from "./compBaseUtil.ts";
import { buildModelValue } from "./compBaseModelValue.ts";
import { buildMisc } from "./compBaseMisc.ts";
import { buildInstance } from "./compBaseInstance.ts";
import { buildProps } from "./compBaseProp.ts";
import { buildEvents } from "./compBaseEvent.ts";
import { buildSlots } from "./compBaseSlot.ts";
import { buildLifeCycle } from "./compBaseLifecycle.ts";
import { WrapPropsType, ContextWrapType } from "./types.ts";
export function useCompBase(props: WrapPropsType, context: SetupContext) {
//Here we have the standard config
//Since some attributes of contextWrap is unavailable, so here call buildContextBasic
//change to computed at 2023/12/18,otherwise the change to props.config will not take affect
const contextBasic = buildContextBasic();
const configStd = computed(() => {
return standardizedConfig(contextBasic, props.config);
});
// console.log("EVAL", JSON.stringify(configStd.value));
//modelValue
const { tryApplyModelValue } = buildModelValue(configStd);
//
const { setComponentInstance, getRef } = buildInstance(configStd);
//contextWrap of this component
const contextWrap = buildContext(contextBasic);
//
const { baseComponent, ifFlag, hasShowFlag, showFlag } = buildMisc(
contextWrap,
configStd
);
//Props,use computed to avoid evaluate each time render is called
const propsReal = buildProps(contextWrap, configStd);
//
const eventsReal = buildEvents(contextWrap, configStd);
//
// eventsReal(propsMost);
//These are props copy
// propsReal(propsMost);
//modelValue should be evaluated every render, so put it into computed
const propsAll = computed(() => {
const all = {
//ref
ref: setComponentInstance,
key: configStd.value["~instanceKey"],
...propsReal.value,
...eventsReal.value,
};
//
tryApplyModelValue(all);
//
return all;
});
//slots
const slots = buildSlots(contextWrap, configStd);
//
//
//The final render function
function wrapRender(): any {
// console.log(ifFlag.value,configStd)
if (!ifFlag.value) {
// console.log('#################',configStd)
return undefined;
}
//
// console.log('render is called',unref(baseComponent), unref(propsAll))
//
const result = h(unref(baseComponent), unref(propsAll), unref(slots));
if (hasShowFlag.value) {
return withDirectives(result, [[vShow, unref(showFlag)]]);
} else {
return result;
}
}
//To avoid JS Hoisting since modelValue is unavailable during this call
function buildContextBasic(): ContextWrapType {
return {
props: getRawValue(props), //Internal use only
context: context, //Internal use only
parent: props.contextParent,
slotPara: props.slotPara,
instanceKey: "",
};
}
//Because of the JS Hoisting, parseConfig can not access contextWrap directly
//Error: can't access lexical declaration 'contextWrap' before initialization
//This is the contextWrap of THIS CompWrap component
function buildContext(contextBasic: ContextWrapType): ContextWrapType {
// return {
// ...contextBasic,
// modelValue,
// getRef,
// instanceKey: configStd.value["~instanceKey"],
// configStd, //Internal use only
// };
//Changed by Jamie @2024/01/16
//Reuse contextBasic so we could get the below attributes from contextBasic after then
//contextBasic.modelValue = modelValue;
contextBasic.getRef = getRef;
contextBasic.instanceKey = configStd.value["~instanceKey"];
contextBasic.configStd = configStd;
//
//
return contextBasic;
}
//
const registerLifeCycles = buildLifeCycle(contextWrap, configStd);
registerLifeCycles();
//
return { wrapRender, contextWrap };
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/jamie0828/mttk-vue-wrap.git
git@gitee.com:jamie0828/mttk-vue-wrap.git
jamie0828
mttk-vue-wrap
mttk-vue-wrap
master

搜索帮助