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> { export default class LazyComponent extends Component<IProps, IState> {
private schema: object | null = null;
constructor(props: IProps) { constructor(props: IProps) {
super(props); super(props);
this.state = { this.state = {
@ -22,9 +20,8 @@ export default class LazyComponent extends Component<IProps, IState> {
public async componentDidMount() { public async componentDidMount() {
const { getPageData } = this.props; const { getPageData } = this.props;
if (getPageData && !this.schema) { if (getPageData && !this.state.schema) {
const schema = await getPageData(); const schema = await getPageData();
this.schema = schema;
this.setState({ schema }); this.setState({ schema });
} }
} }

View File

@ -28,6 +28,7 @@ export default abstract class Provider {
public globalUtils: any = {}; public globalUtils: any = {};
public routerConfig: { [key: string]: string } = {}; public routerConfig: { [key: string]: string } = {};
public layout: { componentName: string; props: any } | null = null; public layout: { componentName: string; props: any } | null = null;
private lazyElementsMap: { [key: string]: any } = {};
constructor() { constructor() {
this.init(); this.init();
@ -66,11 +67,19 @@ export default abstract class Provider {
if (!pageId) { if (!pageId) {
return null; return null;
} }
return createElement(LazyComponent as any, { if (this.lazyElementsMap[pageId]) {
getPageData: async () => await this.getPageData(pageId), console.log('缓存');
key: pageId, return this.lazyElementsMap[pageId];
...props, } 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() { public createApp() {