react-loadable使用跳坑

官方给react-loadable的定义是:

A higher order component for loading components with dynamic imports.

动态路由示例

withLoadable.js

import React from 'react'
import Loadable from 'react-loadable';

export function withLoadable (comp) {
  return Loadable({
    loader: comp,
    loading: () => (
      <div>Loading...</div>
    )
  })
}

 

Root.js

import React from 'react'
import { Provider } from 'react-redux'
import { Route, Switch } from 'react-router-dom'
import { ConnectedRouter } from 'react-router-redux'
import createHistory from 'history/createHashHistory'

import App from './App'
import { withLoadable } from '../components/withLoadable'
const history = createHistory()
const Home = withLoadable(() => import('./Home/Home'))

const Root = ({ store }) => (
  <Provider store={store}>
    <App>
      <ConnectedRouter history={history}>
        <Switch>  
          <Route path="*" component={Home}/>
        </Switch>
      </ConnectedRouter>
    </App>
  </Provider>
)

 

1 style: security content policy报错

当less文件打开sourcemap后,遇到security content policy 报错,屏蔽就好了,现在也没弄明白原因,如遇大神,请指教!

e698e5c984499cbe70a99a8ddbb7529fda9.jpg

{
      test: /\.less$/,
      loader: ExtractTextPlugin.extract(
        Object.assign(
          {
            fallback: {
              loader: require.resolve('style-loader'),
              options: {
                hmr: false
              }
            },

            use: [
              {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                  minimize: true
                  /* 否则security centent policy error */
                  // sourceMap: shouldUseSourceMap
                }
              },
              {
                loader: require.resolve('postcss-loader'),
                options: {
                  ident: 'postcss',
                  plugins: () => [
                    require('postcss-flexbugs-fixes'),
                    autoprefixer({
                      flexbox: true
                    })
                  ]
                }
              },
              {
                loader: 'less-loader',
                options: { modifyVars: theme }
              }
            ]
          },
          extractTextPluginOptions
        )
      )
    }

 

2 createBrowserHistory或者createHashHistory对于react-loadable加载模块路径的影响

当使用createBrowserHistory时,路由跳转会根据当前路径加载模块,假设当前路径为:'localhost:3000/',当跳转到finished时,会加载/finished/static/js/finished_module.js, 从而报错,但应该加载/static/js/finished_module.js,所以,最后使用hash路径

<Provider store={store}>
    <App>
      <ConnectedRouter history={history}>
        <Switch>
          <Route path="/additionalFunc/:cardName"
                 component={AdditionalFunc}/>
          <Route path="/finished" component={Finished}/>
          {/*<Route path="/wisdomWash" component={WisdomWash}/>*/}
          {/*<Route path="/geek" component={Geek}/>*/}
          <Route path="*" component={Home}/>
        </Switch>
      </ConnectedRouter>
    </App>
  </Provider>

 

 

参考资料:https://github.com/jamiebuilds/react-loadable

转载于:https://my.oschina.net/u/2510955/blog/1835333

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值