关于react路由按需加载的问题

我使用的react-loadable在遇到二级路由时会出现打包路径错误的问题,如下图,在路由为xxx.com/recharge-voucher时,工作正常,然后出现二级路径时,打包出来的路径会被加上recharge-voucherReact.Suspense也会遭遇这个问题)

页面显示路径:https://xxx.com/recharge-voucher/static/js/4.f47e7fc7.chunk.js(会导致加载不到文件)
而实际的js文件所在目录是
实际文件:https://xxx.com/static/js/4.f47e7fc7.chunk.js

我不想使用hash方式来解决这个问题

请问怎样配置`react-loadable`才能让打包出来路由js文件路径不含`recharge-voucher`呢?
const AsyncRechargeVoucher = Loadable({
  loader: () => import('./pages/Rechargeoucher'),
  loading: LoadingComponent
})

const RechargeVoucherDetail = Loadable({
  loader: () => import('./pages/RechargeVoucherDetail'),
  loading: LoadingComponent
})

<Auth path="/recharge-voucher" exact component={AsyncRechargeVoucher} />
<Auth path="/recharge-voucher/:id" component={RechargeVoucherDetail} />

clipboard.png

阅读 3.5k
3 个回答

这个不是react-loadable的问题。
改下webapck配置:

output: {
    path: path.resolve(__dirname, './static'),
    publicPath: 'https://uatmer/ewoyin.com/static'
 }

我直接将package.json的中的homepage设置如下"homepage": "/",问题得到了解决,但不知道这样是否会有后遗症?

我也遇到了相同的问题,设置了 "homepage": "/" 和设置 publicPath 都不好用,
不过以下方法解决了问题

// webpack.config.js
import { ReactLoadablePlugin } from 'react-loadable/webpack';

export default {
  plugins: [
    new ReactLoadablePlugin({
      filename: './dist/react-loadable.json'
    }),
  ],
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题