1. import React, { Component } from "react";
    2. export default function asyncComponent(importComponent) {
    3. class AsyncComponent extends Component {
    4. constructor(props) {
    5. super(props);
    6. this.state = {
    7. component: null
    8. };
    9. }
    10. async componentDidMount() {
    11. const { default: component } = await importComponent();
    12. this.setState({component});
    13. }
    14. render() {
    15. const C = this.state.component;
    16. return C ? <C {...this.props} /> : null;
    17. }
    18. }
    19. return AsyncComponent;
    20. }
    1. 使用
    2. const record = asyncComponent(() => import("@/pages/record/record"));
    3. const helpcenter = asyncComponent(() => import("@/pages/helpcenter/helpcenter"));
    4. const production = asyncComponent(() => import("@/pages/production/production"));
    5. const balance = asyncComponent(() => import("@/pages/balance/balance"));