麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

詳解webpack + react + react-router 如何實現懶加載

2019-11-19 14:54:02
字體:
來源:轉載
供稿:網友

在 Webpack 1 中主要是由bundle-loader進行懶加載,而 Webpack 2 中引入了類似于 SystemJS 的System.import語法,首先我們對于System.import的執行流程進行簡單闡述:

  1. Webpack 會在編譯過程中掃描代碼庫時將發現的System.import調用引入的文件及其相關依賴進行單獨打包,注意,Webpack 會保證這些獨立模塊及其依賴不會與主應用的包體相沖突。
  2. 當我們訪問到這些獨立打包的組件模塊時,Webpack 會發起 JSONP 請求來抓取相關的包體。
  3. System.import 同樣也是 Promise,在請求完成之后System.import會將抓取到的模塊作為參數傳入then中的回調函數。
  4. 如果我們重復訪問已經加載完畢的模塊,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呢?

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>  )}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色片网站免费在线观看 | 国产精品成人久久 | 污视频在线免费播放 | 成人黄色短视频在线观看 | 99国产精品自拍 | 日韩av在线资源 | 久色精品视频 | 久久精品视频一区 | 国产成人av在线播放 | 免费观看高清视频网站 | 草草久久久 | 亚洲男人天堂 | 精品中文一区 | 在线成人一区二区 | 黄色免费小网站 | 一级做a爰性色毛片免费 | 久草在线手机视频 | 精品在线观看一区二区三区 | 亚洲免费在线看 | 日韩激情| 青青草成人免费视频在线 | 欧美一级做性受免费大片免费 | 亚洲国产网站 | 黄色伊人网站 | 久久久www成人免费精品 | 国产中出在线观看 | 91在线色视频 | 奶子吧naiziba.cc免费午夜片在线观看 | 中文在线日韩 | 午夜男人在线观看 | 国产免费黄网 | 性欧美极品xxxx欧美一区二区 | 看免费av| 国产羞羞视频 | 成人做爽爽爽爽免费国产软件 | 九九黄色 | 欧美黄色一级片视频 | 天天看天天摸天天操 | 国产精品久久久久久模特 | 精品中文视频 | 国产精品久久久久久238 |