diff --git a/packages/runtime-framework/src/lazyComponent.tsx b/packages/runtime-framework/src/lazyComponent.tsx index b33d0b3df..9acd799ce 100644 --- a/packages/runtime-framework/src/lazyComponent.tsx +++ b/packages/runtime-framework/src/lazyComponent.tsx @@ -11,8 +11,6 @@ interface IState { } export default class LazyComponent extends Component { - private schema: object | null = null; - constructor(props: IProps) { super(props); this.state = { @@ -22,9 +20,8 @@ export default class LazyComponent extends Component { public async componentDidMount() { const { getPageData } = this.props; - if (getPageData && !this.schema) { + if (getPageData && !this.state.schema) { const schema = await getPageData(); - this.schema = schema; this.setState({ schema }); } } diff --git a/packages/runtime-framework/src/provider.ts b/packages/runtime-framework/src/provider.ts index b621c0275..b0535d89d 100644 --- a/packages/runtime-framework/src/provider.ts +++ b/packages/runtime-framework/src/provider.ts @@ -28,6 +28,7 @@ export default abstract class Provider { public globalUtils: any = {}; public routerConfig: { [key: string]: string } = {}; public layout: { componentName: string; props: any } | null = null; + private lazyElementsMap: { [key: string]: any } = {}; constructor() { this.init(); @@ -66,11 +67,19 @@ export default abstract class Provider { if (!pageId) { return null; } - return createElement(LazyComponent as any, { - getPageData: async () => await this.getPageData(pageId), - key: pageId, - ...props, - }); + if (this.lazyElementsMap[pageId]) { + console.log('缓存'); + return this.lazyElementsMap[pageId]; + } else { + const lazyElement = createElement(LazyComponent as any, { + getPageData: async () => await this.getPageData(pageId), + key: pageId, + ...props, + }); + this.lazyElementsMap[pageId] = lazyElement; + console.log('新组件'); + return lazyElement; + } } public createApp() {