时间:2021-05-25
在 Webpack 1 中主要是由bundle-loader进行懒加载,而 Webpack 2 中引入了类似于 SystemJS 的System.import语法,首先我们对于System.import的执行流程进行简单阐述:
而 React Router 路由的懒加载实际上分为动态路由与与懒加载两步,典型的所谓动态路由配置如下:
/** * <Route path="/" component={Core}> * <IndexRoute component={Home}/> * <Route path="about" component={About}/> * <Route path="users" component={Users}> * <Route path="*" component={Home}/> * </Route> */export default { path: '/', component: Core, indexRoute: { getComponent(location, cb) { ... }, }, childRoutes: [ { path: 'about', getComponent(location, cb) { ... }, }, { path: 'users', getComponent(location, cb) { ... }, }, { path: '*', getComponent(location, cb) { ... }, }, ],};正常打包
import IndexPage from './views/app.jsx'import AboutPage from './views/about.jsx'export default function({history}) { return ( <Router history={history}> <Route path="/" component={IndexPage} /> <Route path="/about" component={AboutPage} /> </Router> )}这是一个正常打包的路由写法, 如果需要分割代码, 我们需要改造下路由, 借助getComponent和require.ensure
webpack 代码分割
export default function({history}) { return ( <Router history={history}> <Route path="/" getComponent={(location, callback) => { require.ensure([], function(require) { callback(null, require('./HomePage.jsx')) }) }} /> <Route path="/about" getComponent={(location, callback) => { require.ensure([], function(require) { callback(null, require('./AboutPage.jsx')) }) }} /> </Router> )}这样看来代码有点累, 我们稍微改造下
const home = (location, callback) => { require.ensure([], require => { callback(null, require('./HomePage.jsx')) }, 'home')}const about = (location, callback) => { require.ensure([], require => { callback(null, require('./AboutPage.jsx')) }, 'about')}export default function({history}) { return ( <Router history={history}> <Route path="/" getComponent={home}></Route> <Route path="/about" getComponent={about}></Route> </Router> )}这样看起来是不是简洁了很多
注意: 由于webpack的原因, 如果直接require('./AboutPage.jsx')不能正常加载, 请尝试require('./AboutPage.jsx').default
webpack2 代码分割
上面的代码看起来好像都是webpack1的写法, 那么webpack2呢?
webpac2就需要借助System.import了
export default function({history}) { return ( <Router history={history}> <Route path="/" getComponent={(location, callback) => { System.import('./HomePage.jsx').then(component => { callback(null, component.default || component) }) }} /> <Route path="/about" getComponent={(location, callback) => { System.import('./AboutPage.jsx').then(component => { callback(null, component.default || component) }) }} /> </Router> )}我们一样可以把上面的代码优化一下
const home = (location, callback) => { System.import('./HomePage.jsx').then(component => { callback(null, component.default || component) })}const about = (location, callback) => { System.import('./AboutPage.jsx').then(component => { callback(null, component.default || component) })}export default ({ history }) => { return ( <Router history={history}> <Route name="home" path="/" getComponent={home} /> <Route name="about" path="/about" getComponent={about} /> </Router> )}webpack2 + dva 实现路由和 models 懒加载
const routerThen = (app, callback, [component, model]) => { app.model(model.default || model) callback(null, component.default || component)}export default ({ history, app }) => { return ( <Router history={history}> <Route name="home" path="/" getComponent={(location, callback) => { Promise.all([ System.import('./views/app.jsx'), System.import('./models/topics') ]).then(routerThen.bind(null, app, callback)) }} /> <Route name="article" path="/article/:id" getComponent={(location, callback) => { Promise.all([ System.import('./views/article.jsx'), System.import('./models/topic') ]).then(routerThen.bind(null, app, callback)) }} /> </Router> )}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
简要介绍:在React-router中,暴露了3个接口,如果结合webpack的codesplitting,就通过切换路由实现按需加载。1.webpack的co
本文主要介绍的是使用React-router和Webpack如何快速构建一个react程序,下面话不多说,感兴趣的可以一起学习学习。初始化项目我们先创建个空文件
react-router模块化配置因为公司的需要最近踏进了react坑,一直在挖坑填坑,在路由这一块折腾得不行。直接进入主题,配置react-router模块化
本文介绍了React从react-router路由上做登陆验证控制的方法,分享给大家,具体如下:验证代码importReactfrom'react'import
前言本文主要给大家介绍了关于react-router跳转传值的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:react-router跳转传值1.引