feat: cache lazyElement

This commit is contained in:
wuyue.xht 2020-03-17 12:23:28 +08:00
parent ef99ec27dc
commit 8f3b4e69af
2 changed files with 15 additions and 9 deletions

View File

@ -11,8 +11,6 @@ interface IState {
}
export default class LazyComponent extends Component<IProps, IState> {
private schema: object | null = null;
constructor(props: IProps) {
super(props);
this.state = {
@ -22,9 +20,8 @@ export default class LazyComponent extends Component<IProps, IState> {
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 });
}
}

View File

@ -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() {