蝙蝠岛资源网 Design By www.hbtsch.com
在 Webpack 1 中主要是由bundle-loader进行懒加载,而 Webpack 2 中引入了类似于 SystemJS 的System.import语法,首先我们对于System.import的执行流程进行简单阐述:
- Webpack 会在编译过程中扫描代码库时将发现的System.import调用引入的文件及其相关依赖进行单独打包,注意,Webpack 会保证这些独立模块及其依赖不会与主应用的包体相冲突。
- 当我们访问到这些独立打包的组件模块时,Webpack 会发起 JSONP 请求来抓取相关的包体。
- System.import 同样也是 Promise,在请求完成之后System.import会将抓取到的模块作为参数传入then中的回调函数。
- 如果我们重复访问已经加载完毕的模块,Webpack 不会重复执行抓取与解析的过程。
而 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呢"htmlcode">
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>
)
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无详解webpack + react + react-router 如何实现懒加载的评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2025年10月28日
2025年10月28日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]