test: 渲染器应用深度监听当入参发生变化时重新渲染组件

This commit is contained in:
wangxuefeng 2025-03-31 11:23:19 +08:00
parent 2df2e3cffb
commit a64433e57b

View File

@ -266,26 +266,32 @@ const setupWujiePropsWatcher = () => {
//
prevPropsSnapshot = currentPropsSnapshot;
// initParams
const newParams = window.$wujie.props as WujieProps;
// props
const newProps = JSON.parse(currentPropsSnapshot);
const oldParams = initParams.value;
//
initParams.value = newParams;
//
initParams.value = {
...newProps,
_timestamp: Date.now(), //
};
console.log('initParams', initParams.value);
console.log('initParams 已更新为新对象:', initParams.value);
//
if (
isValidParams(newParams) &&
(oldParams?.fileId !== newParams.fileId ||
oldParams?.projectId !== newParams.projectId)
isValidParams(initParams.value) &&
(oldParams?.fileId !== initParams.value.fileId ||
oldParams?.projectId !== initParams.value.projectId)
) {
console.log('核心参数发生变化,重新加载组件');
reloadComponent();
} else {
// 使
forceUpdateRenderer();
}
}
}, 300); // 300ms
}, 300);
//
onBeforeUnmount(() => {
@ -293,6 +299,25 @@ const setupWujiePropsWatcher = () => {
});
};
//
const forceUpdateRenderer = () => {
if (renderer.value && typeof renderer.value.update === 'function') {
// update
console.log('调用渲染器的 update 方法');
renderer.value.update(initParams.value);
} else {
//
console.log('强制刷新渲染器组件');
const temp = renderer.value;
renderer.value = null;
//
setTimeout(() => {
renderer.value = temp;
}, 0);
}
};
//
const reloadComponent = async () => {
if (!isValidParams(initParams.value)) {
@ -383,8 +408,10 @@ onMounted(async () => {
<template>
<div class="renderer-container">
<renderer
<component
:is="renderer"
v-if="renderer"
:key="initParams?._timestamp || 'default'"
:ctx-props="initParams"
v-bind="initParams"
style="width: 100%; height: 100%"