issue: react-loadable 怎么加入这个骨架中 #23

安装依赖

  1. npm install react-loadable --save

实现异步组件

  1. // async-image.jsx
  2. import React, { Component } from 'react';
  3. import Logo from '../../../../asset/images/react.png';
  4. export default class AsyncImage extends React.Component {
  5. render() {
  6. return <div><img src={Logo} /><div>这是异步加载的内容</div></div>
  7. }
  8. }
  9. // loading.jsx
  10. import React, { Component } from 'react';
  11. export default class Loading extends React.Component {
  12. render() {
  13. return <div>Loading.....</div>
  14. }
  15. }

引入异步组件

  1. import React, { Component } from 'react';
  2. import Loadable from 'react-loadable';
  3. import Loading from './widget/loading';
  4. const AsyncImageLoadableComponent = Loadable({
  5. loader: () => import('./widget/async-image'),
  6. loading: Loading,
  7. });
  8. export default class About extends Component {
  9. render() {
  10. return <div>
  11. <h3 className="spa-title">Egg + React + Redux + React Router SPA Server Side + Webpack Render Example</h3>
  12. <div style={{ 'text-align': 'center' }}><AsyncImageLoadableComponent /></div>
  13. </div>;
  14. }
  15. }

本地开发模式异步模块找不到问题

image.png

解决方案:easywebpack 在开发模式构建时,筛选出异步chunk 模块,然后落地磁盘


具体例子见:https://github.com/easy-team/egg-react-webpack-boilerplate/blob/dynamic-import/app/web/page/app/components/router/about.jsx